ansible_流程控制和roles

ansible_third

知識回顧

模塊
yum 安裝 yum模塊python

  • name @ 安裝包組

pip 安裝python相關模塊linux

  • name
  • requirements 用來指定文件安裝

service 啓動服務nginx

  • name 服務名稱
  • state
    • started
    • stopped
    • restarted
    • reloaded
  • enabled 設置開機自啓動

cron

  • minute
  • hour
  • day
  • mouth
  • weekday
  • job state
  • disable = yes

不能都是* 那也會致使每分鐘都執行相同的命令 會撐爆服務器web

user

  • groupredis

  • groupsshell

  • stateubuntu

  • namecentos

  • home服務器

  • shell架構

  • uid

  • password

  • remove = yes

group 建立組

  • name
  • gid
  • system

user 用戶

  • 超級用戶 0
  • 普通用戶
    • 系統用戶 centos7 1-999 centos6 1-499
    • 系統用戶 centos7 1000-65535 centos 500-65535

AD-hoc 這些模塊是這樣稱呼的

yaml

  • 字典 key: value 冒號後面必須有空格
  • 列表 - []
  • 後綴名 yaml yml

傳參 動態的

  • -e
  • host文件[groupname: vars]
  • playbook register 註冊
  • playbook vars
  • hosts 文件裏面 主機地址後面寫
  • -e > playbook > hosts

yaml

  • 冒號後面必須有空格
  • 等號後面不能有空格
  • -後面也要有空格
  • 嚴格對齊

new_content

setup

ansible all -m setup


ansible_all_ipv4_addresses 
# ipv4的全部地址

ansible_all_ipv6_addresses 
# ipv6 的全部地址

ansible_date_time
# 獲取到節點控制時間

ansible_default_ipv4
# 默認的ipv4地址

ansible_distribution 
# 系統

ansible_distribution_major_version
# 系統的版本號

ansible_domain
# 系統所在的域

ansible_env 
# 系統的環境變量

ansible_hostname 
# 系統的主機名

ansible_fqdn
# 系統的全名

ansible_machine 
# 系統的架構

ansible_memory_mb
# 系統的內存信息

ansible_os_family 
# 系統的家族

ansible_pkg_mgr
# 系統的包管理工具

ansible_processor_cores 
# 系統的cpu的核數(每顆)

ansible_processor_count
# 系統cpu的顆數

ansible_processor_vcpus
# 系統cpu的總個數= core * count 核數乘顆數

ansible_python 
# 系統上的python


ansible cache -m setup -a 'filter=*processor*'
# 用來搜索filter後面的字段

*匹配數量,表示(0,+%)

? 匹配數量,即是 0 | 1 兩者之一

.除了換行符之外的全部字符

+至少一次(1,+%)

[12365] 匹配內容,存在便可 or 關係

() 分組

{m,}次數,出現m次

{m,}至少m次

{m,n}出現m-n次

a*.b 能夠匹配db

a.*匹配不了db

條件判斷

  • 不一樣的系統
  • 不一樣的版本
  • 不一樣的環境
  • 不一樣的用戶
- hosts: db
  remote_user: root
  tasks:
  - name: createfile
  copy: content='風蕭蕭兮易水寒' dest=/tmp/a.txt
  when: a=='3'
  - name: createfile
  copy: content='壯士一去兮不復還' dest=/tmp/a.txt
  when: a=='4'

Tips: Ubuntu安裝包是apt-get

tags

- hosts: db
  tasks:
  - name: wadong
    tieqiao: wadong
  - name: tk
    dong: tk
    tags: tk
- hosts: web
  tasks: 
  - name: install_nginx
    yum: name=nginx
  - name: copyfile
    copy: src=/etc/nginx/nignx.conf dest=/etc/nginx/nginx.conf
    tags: copyfile
  - name: start
    service: mame=nginx state=started
ansible-playbook -t copyfile p1.py
# 只執行標籤的動做 其他的不執行

循環 with_item

一次性建立多個

- hosts: gb
  tasks:
  - name: gbqc
    dong: {{ item }}
    with_items:
    - qbqc
    - cyf
    - ssfj
- hosts: web
  task:
  - name: createuser
    user: name={{item}}
    with_items:
    - max20
    - max21
    - max22
- host: web
  tasks:
  - name: createuser
    user: name={{ item }}
    with_items:
    - eral11
    - eral12
    - eral13

循環嵌套

- hosts: web
  tasks:
  - name: crategroup
    group: name={{ item }}
    with_items:
    - max30
    - max31
    - max32
   - name: createuser
     user: name={{ item.name}} group={{ item.group }}
     with_items:
     - {'name':eral11,'group':sex11}
     - {'name':eral12,'group':sex12}
     - {'name':eral13,'group':sex13}

template

jinja2

- hosts: web
  tasks: 
  - name: installredis
    yum: name=redis
  - name: copyfile
    template: src=/etc/redis.conf dest=/etc/redis.conf
  - name: start
    service: name=redis state=started
  
  
 配置文件:bind {{ansible_default_ipv4.address }}

copy和template的區別

  • copy模塊不替代參數
  • template 模塊替代參數
- hosts: web
  tasks:
  - name: installredis
    yum: name=redis
  - name: copyfile
    template: src=redis.conf dest=/etc/redis.conf
  - name: start
    service: name=redis state=started

tips: 能夠寫相對路徑,在當下目錄新建一個templates目錄,

而後把文件放在templates目錄中

handlers

修改配置文件

- hosts: web
  tasks:
  - name: installredis
    yum: name=redis
  - name: copyfile
    template: src=redis.conf dest=/etc/redis.conf
    tags:copyfile
    nofity:restart
  - name:start
    service: name=redis state=restarted
  handlers:
  - name: restart
    service: name=redis state=restarted

roles

  • 目錄清晰
  • 能夠相互調用

roles 文件夾

文件夾裏面是要建立的每個角色,每個文件夾

每個角色裏面都要有tasks(must),templates,files,handlers,

vars目錄

每一個目錄都要有main.yml文件,經過import_tasks來調用

其中templates文件夾中的文件能夠經過相對路徑來調用

其中files文件夾是否能夠經過相對路徑來調用

hodoop 大數據

setenforce 0
# 用來臨時關閉selinux
iptables -F
# 臨時關閉防火牆
# 這個須要yum install iptables
/etc/seliunx/config 
# 永久關閉
相關文章
相關標籤/搜索