一 setup
輸入命令 ansible all -m setup #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_distribution_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的總個數=cpu的顆數*CPU的核數 ansible_python # 系統上的python ansible cache -m setup -a 'filter=*processor*' # 用來搜索
二 條件判斷
不同條件 不同版本 不同環境 不同用戶
boo4.yml
- hosts: db
remote_user: root
tasks:
- name: createfile
copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
when: a=="3"
- name: cratefile
copy: content="小弦切切如私語" dest=/tmp/a.txt
when: a=="4"
運行: ansible-playbook -e 'a=="3"' book4.yml
注意 -e 代表傳參
三 tags
book5.yml
- hosts: web
tasks:
- name: installnginx
yum: name=nginx
- name: copyfile
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: copyfile
- name: start
service: name=nginx state=started
運行時:
ansible-playbook -t copyfile book5.yml
輸入 ansible-playbook 可以查看它的參數
四 循環with_item
一次創建多個
book7.yml
- hosts: web
tasks:
- name: crateuser
user: name={{item}}
with_items:
- mlh20
- mlh21
- mlh22
ansible-playbook book7.yml 執行
book7.yml
- hosts: web
tasks:
- name: crateuser
user: name={{item}}
with_items:
- alex30
- alex31
- alex32
- name: crategroup
group: name={{item}}
with_items:
- wulaoshi20
- wulaoshi21
- wulaoshi22
