ansible之setup、條件判斷、tags、循環handlers


一、setup

ansible all -m setup 查詢出所有的信息

過濾:ansible all -m setup -a "filter=ansible_os_family"

 1 ansible_all_ipv4_addresses # ipv4的所有地址
 2 ansible_all_ipv6_addresses # ipv6的所有地址
 3 ansible_date_time # 獲取到控制節點時間
 4 ansible_default_ipv4 # 默認的ipv4地址
 5 ansible_distribution # 系統
 6 ansible_distribution_major_version # 系統的大版本
 7 ansible_distribution_version # 系統的版本號
 8 ansible_domain #系統所在的域
 9 ansible_env #系統的環境變量
10 ansible_hostname #系統的主機名
11 ansible_fqdn #系統的全名
12 ansible_machine #系統的架構
13 ansible_memory_mb #系統的內存信息
14 ansible_os_family # 系統的家族
15 ansible_pkg_mgr # 系統的包管理工具
16 ansible_processor_cores #系統的cpu的核數(每顆)
17 ansible_processor_count #系統cpu的顆數
18 ansible_processor_vcpus #系統cpu的總個數=cpu的顆數*CPU的核數
19 ansible_python # 系統上的python
20 ansible cache -m setup -a 'filter=*processor*' # 用來搜索
View Code

 

二、條件判斷

1 - hosts: db
2   remote_user: root
3   tasks:
4   - name: createfile
5     copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
6     when: a=="3"
7   - name: cratefile
8     copy: content="小弦切切如私語" dest=/tmp/a.txt
9     when: a=="4"

 

三、tags 可以指定執行部分任務

 1 - hosts: web
 2   tasks:
 3   - name: installnginx
 4     yum: name=nginx
 5   - name: copyfile
 6     copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
 7     tags: copyfile
 8   - name: start
 9     service: name=nginx state=started
10     

 

四、循環 with_items

1 - hosts: web
2   tasks:
3   - name: crateuser
4     user: name={{item}}
5     with_items:
6     - user1
7     - user2
8     - user3

嵌套循環

 1 - hosts: web
 2   tasks:
 3   - name: crategroup
 4     group: name={{item}}
 5     with_items:
 6     - wulaoshi30
 7     - wulaoshi31
 8     - wulaoshi32
 9   - name: createuser
10     user: name={{item.name}} group={{item.group}} # 循環每個字典取值
11     with_items:
12     - {'name':alex40,'group':wulaoshi30} 
13     - {'name':alex41,'group':wulaoshi31}
14     - {'name':alex42,'group':wulaoshi32}

 

template模塊
template模塊和copy模塊的區別

  • copy模塊不替代參數

  • template模塊替代參數

1 - hosts: web
2   tasks:
3   - name: installredis
4     yum: name=redis
5   - name: copyfile
6     template: src=redis.conf dest=/etc/redis.conf
7   - name: start
8     service: name=redis state=started

在本機的redis的配置文件里bind 的ip隨被控管機改變而改變

bind {{ ansible_default_ipv4.address }}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM