1. 程式人生 > >Ansible中的劇本 ansible--playbook

Ansible中的劇本 ansible--playbook

ansible mask start 系統時間 std reat 基於 dom fault

playbook格式

Usage: ansible-playbook [options] playbook.yml [playbook2 ...]

-C, --check #白跑,執行但是不會有結果
--list-hosts #列出符合的主機
-f FORKS, --forks=FORKS #做並發
--syntax-check #檢查語法
-k, --ask-pass #輸入密碼

單個playbook

- hosts: web
  remote_user: root
  tasks:
  - name: createuser
    user: name=alex20 home=/opt/alex20 uid=4000

多個playbook

- hosts: web
  remote_user: root
  tasks:
  - name: createuser
    user: name=alex20 home=/opt/alex20 uid=4000 
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs

冪等性 不管執行多少次,得到的結果都是一樣的

- hosts: web
  remote_user: root
  tasks:
  - name: createuser
    user: name=tom20 home=/opt/tom20 uid=4000
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs
- hosts: db
  tasks:
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs                         

傳參

第一種方式

- hosts: web
  tasks:
  - name: create{{user}}
    user: name={{user}}
ansible-playbook -e user=wusir20 p3.yml

第二種方式

[web]
192.168.226.[101:102] user=alex30
192.168.226.104  user=alex100

第三種方式

[web:vars]
user=alex31

第四種方式

- hosts: web
  vars:
  - user: alex32
  tasks:
  - name: create{{user}}
    user: name={{user}}

第五種傳參方式

- hosts: web
  tasks:
  - name: yum
    yum: name=bc
  - name: sum
    shell: echo 11+22|bc
    register: user
  - name: echo
    shell: echo {{user.stdout}} > /tmp/echo.txt 
  - name: create{{user.stdout}}
    user: name=tom{{user.stdout}}

優先級

-e > playbook > hosts

setup

ansible_all_ipv4_addresses #所有的ipv4地址
ansible_all_ipv6_addresses #所有的ipv6地址
ansible_architecture #系統的架構
ansible_date_time #系統時間
ansible_default_ipv4 #默認的ipv4地址
    address ip地址
    alias 網卡名稱
    broadcast 廣播地址
    gateway 網關
    netmask 子網掩碼
    network 網段
ansible_default_ipv6 #默認的ipv6地址
ansible_device_links #系統的磁盤信息
ansible_distribution #系統名稱
ansible_distribution_file_variety #系統的基於公司
ansible_distribution_major_version #系統的主版本
ansible_distribution_version #系統的全部版本
ansible_dns #系統的dns 默認udp 端口53
ansible_domain #系統的域 ldap
ipv4 #ipv4地址
ansible_env #系統的環境
ansible_fqdn #系統的完整主機名
ansible_hostname #系統的簡寫主機名
ansible_kernel #系統的內核版本
ansible_machine #系統的架構
ansible_memtotal_mb #系統的內存
ansible_memory_mb #系統的內存使用情況
ansible_mounts #系統的掛載信息
ansible_os_family #系統家族
ansible_pkg_mgr #系統的包管理工具
ansible_processor #系統的cpu
ansible_processor_cores #每顆cpu的核數
ansible_processor_count #cpu的顆數
ansible_processor_vcpus #cpu的個數=cpu的顆數*每顆cpu的核數
ansible_python #系統python信息
ansible_python_version #系統python的版本
ansible_system #系統名字

tags

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
  - name: start
    service: name=redis state=started
ansible-playbook -t copy p7.yml

handlers

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted

template

絕對路徑

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

mv redis.conf{,.j2} = mv redis.conf redis.conf.j2

相對路徑

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    template: dest=/etc/redis.conf src=redis.conf.j2
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted
在當前目錄下創建一個templates的目錄,就可以使用相對路徑

yy 復制一行

# yy 復制多行

p 粘貼

dd 刪除一行

# dd 刪除多行

d$ 從當前位置刪除到結尾

when

- hosts: web
  tasks:
  - name: copyfile
    copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
    when: ansible_distribution_major_version=="7"
  - name: copyfile
    copy: content="小弦切切如私語" dest=/tmp/a.txt
    when: ansible_distribution_major_version=="6"
- hosts: web
  tasks:
  - name: copyfile
    copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
    when: user=="4"
  - name: copyfile
    copy: content="小弦切切如私語" dest=/tmp/a.txt
    when: user=="3"

with_items

- hosts: web
  tasks:
  - name: createuser
    user: name={{item}}
    with_items:
    - tom50
    - Robert50
    - mike50
- hosts: web
  tasks:
  - name: createuser
    user: name={{item}}
    with_items:
    - alex51
    - wusir51
    - taibai51
  - name: creategroup
    group: name={{item}}
    with_items:
    - alex60
    - wusir60
    - taibai60

嵌套循環

- hosts: web
  tasks:
  - name: crateuser
    user: name={{item.name}}  group={{item.group}}
    with_items:
    - {"name":tom52,"group":tom60}
    - {"name":BOB52,"group":BOB60}
    - {"name":mike52,"group":mike60}

Ansible中的劇本 ansible--playbook