ansible-plabook 之 when 判斷


條件判斷

  • when的值是一個條件表達式,如果條件判斷成立,這個task就執行,如果判斷不成立,則task不執行
  • 如果需要根據變量、facts(setup)或此前任務的執行結果來作為某task執行與否的前提時要用到條件測試,在Playbook中條件測試使用when子句。
  • 在task后添加when子句即可使用條件測試:when子句支持jinjia2表達式或語法,例如:
  • 1 - name: touch centos
    2   command: touch /home/123.test
    3   when:
    4     - ansible_distribution == "CentOS"
    5     - ansible_distribution_major_version == "7"
    6   tags:
    7   - touch_Cent
    • 條件一:為Centos 系統
    • 條件二:版本號為7
    • 滿足這兩個條件會在 /home/ 創建 123.test 文件
  • 組條件判斷
  • - name: "CentOS 6 and Debian 7 systems"
      command: mkdir  /home/test_new/CentOS -p
      when: (ansible_distribution ==  "CentOS" and ansible_distribution_major_version == "7") or
            (ansible_distribution ==  "Debian" and ansible_distribution_major_version == "6")
      tags:
      - mkdir_Cent
  • 自定義條件判斷
  • 迭代  
  • 有需要重性執行的任務時,可以使用迭代機制。其使用格式為將需要迭代的內容定義為item變量引用,並通過with_items語句指明迭代的元素列表即可。
  • 示例:  

  

- name: Install packages
  yum: name={{ item }} state=present
  with_items:
    - telnet
    - net-tools
    - perl-XML-DOM
    - perl-Switch
  tags:
  - yum

 

創建用戶組:

- name: "Add groups"
  group: name={{ item.name }} state=present
  with_items:
    - { name: "zhang" }
    - { name: "zhao" }
  tags:
  - Add_group

創建用戶:

- name: "Add Users"
  user: name={{ item.name }} state=present groups={{ item.groups }}
  with_items:
    - { name: "hu", groups: "zhang" }
    - { name: "zhi", groups: "zhao" }
  tags:
  - Add_users

刪除用戶:

- name: "userdel user"
  user: name={{ item.name }} state=absent  remove=yes
  with_items:
    - { name: "hu" }
    - { name: "zhi" }
    - { name: "zhangsan" }
    - { name: "lisi" }
    - { name: "zhaosi" }
    - { name: "zhangsi" }
    - { name: "test1" }
  tags:
  - Del_Users 

 

 

  • Templates介紹
  • Jinja是基於Python的模板引擎。template類是Jinja的另一個重要組件,可以看作一個編譯過的模塊文件,用來生產目標文本,傳遞Python的變量給模板去替換模板中的標記。

# scp root@192.168.92.139:/etc/httpd/conf/httpd.conf ./templates      //復制被管理端的配置文件到本地
# vim templates/httpd.conf      //在管理端講配置文件要修改的地方定義變量
Listen {{http_port}}
ServerName {{server_name}}  
MaxClients {{access_num}}

  在/etc/ansible/hosts 添加變量

# vim /etc/ansible/hosts
[abc]
192.168.200.129 http_port=192.168.92.139:80 access_num=100 server_name="www.xxxxxx.com:80"
# vim apache.yml
# ansible-playbook twsdyxz.web.yml --tags=copy_httpd.conf.j2    #然后執行腳本  然后去abc組的主機上查看下配置文件是否已經改了

 

vim twsdyxz.web.yml
########################################################
- hosts: abc
  remote_user: root
  vars:
    - package: httpd
    - service: httpd
  become: yes                #2.6版本以后的參數,之前是sudo,意思為切換用戶運行
  become_user: root          #指定sudo用戶為mysql
  roles:
  - twsdyxz
  tags:
  - only0

 

 

 

vim tasks/main.yml
###########################################3
- name: "install configure file"
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  tags:
  - copy_httpd.conf.j2

 


免責聲明!

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



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