• [ansible] playbook运用


    一、复习playbook剧本

    1. ---
    2. - name: first play for install nginx #设置play的名称
    3. gather_facts: false #设置不收集facts信息
    4. hosts: webservers:dbservers #指定执行此play的远程主机组
    5. remote_user: root #指定执行此play的用户
    6. tasks: #指定此play的任务列表
    7. - name: disabled firewalld
    8. service: name=firewalld state=stopped enabled=no
    9. - name: disable selinux
    10. command: '/sbin/setenforce 0'
    11. ignore_errors: yes
    12. - name: disabled selinux forever
    13. replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
    14. - name: mount cdrom
    15. mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
    16. - name: install pkgs
    17. yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest
    18. - name: create nginx user
    19. user: name=nginx create_home=no shell=/sbin/nologin
    20. - name: unarchive nginx package
    21. unarchive: copy=yes src=/etc/ansible/nginx/nginx-1.24.0.tar.gz dest=/opt/
    22. - name: install nginx by source
    23. shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
    24. - name: create link file for nginx
    25. file: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx
    26. - name: create nginx service file
    27. copy: src=nginx.service dest=/lib/systemd/system/nginx.service
    28. - name: start nginx
    29. service: name=nginx state=started enabled=yes

     

     

    二、playbook的定义、引用变量

    2.1 基础变量的定义与引用

    在yaml文件中,我们可以在初始配置的模块中用var去定义变量的存在,变量的格式为key:value,以此来确定该变量在剧本中的存在

    1. vim test1.yaml
    2. ---
    3. - name: this is a play for testing variables
    4. hosts: dbservers
    5. remote_user: root
    6. vars:
    7. filename: abc.txt
    8. tasks:
    9. - name: touch a test file
    10. file: path=/opt/{{filename}} state=touch
    11. ansible-playbook test1.yaml

     

    2.2 引用fact信息中的变量  

    首先我们知道  使用 ansible 组  -m setup   可以收集该组中所有的节点信息 ,

    所以setup中fact'信息,有时候会剧本编写中需要,而fact的信息也是可以通过变量的方式进行调用

    1. vim test2.yaml
    2. ---
    3. - name: this is a playbook for quote variate
    4. hosts: dbservers
    5. remote_user: root
    6. tasks:
    7. - name: reading setup fact variate
    8. debug: msg={{ansible_date_time.weekday}}
    9. ~

     

    三、playbook中的when条件判断和变量循环使用 

    3.1 when条件判断 

    1. #选用filter=ansible_default_ipv4中的address作为when条件进行测试
    2. ansible all -m setup -a 'filter=ansible_default_ipv4'

    1. vim test3.yaml
    2. ---
    3. - name: this is when test playbook
    4. hosts: all
    5. remote_user: root
    6. tasks:
    7. - name: test when
    8. debug: msg='判断位置'
    9. when: ansible_default_ipv4.address == "192.168.136.198"
    10. ansible-playbook test3.yaml

     

    除此之外 when条件还可以通过 !=(不等于条件来进行判断) 

    1. vim test3.yaml
    2. ---
    3. - name: this is when test playbook
    4. hosts: all
    5. remote_user: root
    6. tasks:
    7. - name: test when
    8. debug: msg='判断位置'
    9. when: ansible_default_ipv4.address != "192.168.136.198"
    10. ansible-playbook test3.yaml

    四、变量循环 

    (1)with_item 单循环输出
    1. vim test4.yaml
    2. ---
    3. - name: item test
    4. hosts: dbservers
    5. remote_user: root
    6. gather_facts: no
    7. tasks:
    8. - debug:
    9. msg: "{{item}}"
    10. with_items: [a, b, c, d]
    11. ansible-playbook test4.yaml

     

     当列表为两个时。with_item的输出方式:

    1. vim test4.yaml
    2. ---
    3. - name: item test
    4. hosts: dbservers
    5. remote_user: root
    6. gather_facts: no
    7. tasks:
    8. - debug:
    9. msg: "{{item}}"
    10. with_items:
    11. - [a, b, c, d]
    12. - [1 ,2, 3, 4]
    13. ansible-playbook test4.yaml

     

    (2)with_list  每组列表一起循环的输出 
    1. ---
    2. - name: item test
    3. hosts: dbservers
    4. remote_user: root
    5. gather_facts: no
    6. tasks:
    7. - debug:
    8. msg: "{{item}}"
    9. with_list:
    10. - [a, b, c, d]
    11. - [1 ,2, 3, 4]
    12. ~
    13. ~

    (3)with_together 同一列表位置数据组合输出的循环 

    1. ---
    2. - name: item test
    3. hosts: dbservers
    4. remote_user: root
    5. gather_facts: no
    6. tasks:
    7. - debug:
    8. msg: "{{item}}"
    9. with_together:
    10. - [a, b, c, d]
    11. - [1 ,2, 3, 4]
    12. ~

     

    1. ---
    2. - name: item test
    3. hosts: dbservers
    4. remote_user: root
    5. gather_facts: no
    6. tasks:
    7. - debug:
    8. msg: "{{item}}"
    9. with_together:
    10. - [a, b, c, d]
    11. - [1 ,2, 3, 4]
    12. - [A, B, C]

     

    (4) with_nested 列表数据循环匹配的循环(根据列表个数定义有多少层的循环) 
    1. ---
    2. - name: item test
    3. hosts: dbservers
    4. remote_user: root
    5. gather_facts: no
    6. tasks:
    7. - debug:
    8. msg: "{{item}}"
    9. with_nested:
    10. - [a, b, c, d]
    11. - [1 ,2, 3, 4]
    12. ~

     

    四种迭代循环方式的总结

    whith_items:  {{item}}会把所有的列表展开进行遍历输出,with_flattened也可以替代with_items

     with_list:    {{item}}会把每个列表当作一个整体输出。如果每个列表中只有一个值,则效果与with items一致。loop也可以替代ith

     with_together: {{item}}引用时会把每个列表相同位置的值对齐合并后输出

    with nested:{ {item}}引用时会把每个列表的值两两组合循环输出

    五、Templates 模块

    Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。 

     本次我们以改变apche的配置文件为例,来展现Templates模块的运用

    (1)先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量 
    1. #如果没有相关的httpd的配置文件,可以先yum按住一个httpd的服务,取其主配置文件
    2. cp httpd.conf /etc/ansible/httpd/httpd.conf.j2
    3. vim /etc/ansible/httpd/httpd.conf.j2
    4. Listen {{http_port}} #42行,修改
    5. ServerName {{server_name}} #95行,修改
    6. DocumentRoot "{{root_dir}}" #119行,修改

    (2) 修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量 
    1. vim /etc/ansible/hosts
    2. [webservers]
    3. 192.168.136.197 http_port=192.168.136.197:80 server_name=www.test1.com:80 root_dir=/etc/httpd/htdocs
    4. [dbservers]
    5. 192.168.136.198 http_port=192.168.136.198:80 server_name=www.test2.com:80 root_dir=/etc/httpd/htdocs

     此外如果没有做DNS解析域名,还需要对主机名进行映射 :

    1. vim /etc/hosts
    2. 192.168.73.106 www.test1.com
    3. 192.168.73.107 www.test2.com
    (3)编写 playbook 
    1. mkdir /etc/ansible/templates
    2. vim apache.yaml
    3. ---
    4. - hosts: all
    5. remote_user: root
    6. vars:
    7. - package: httpd
    8. - service: httpd
    9. tasks:
    10. - name: install httpd package
    11. yum: name={{package}} state=latest
    12. - name: install configure file
    13. template: src=/etc/ansible/httpd/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    14. notify:
    15. - restart httpd
    16. - name: create root dir
    17. file: path=/etc/httpd/htdocs state=directory
    18. - name: start httpd server
    19. service: name={{service}} enabled=true state=started
    20. handlers:
    21. - name: restart httpd
    22. service: name={{service}} state=restarted
    23. ansiable-playbook apache.yaml

     

    ​​​​​​​

    六、Tags 

    可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
    playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。

    6.1 单标签的使用

    1. vim test10.yaml
    2. ---
    3. - name: this is a play for testing variables
    4. hosts: dbservers
    5. remote_user: root
    6. vars:
    7. filename: abc.txt
    8. tasks:
    9. - name: position 1
    10. debug:
    11. msg: 'ls /opt'
    12. tags:
    13. - only
    14. - name: position 2
    15. debug:
    16. msg: 'ls /mnt'
    17. ansible-playbook test1.yaml --tags="only"

    6.2 多标签的运用

    1. ---
    2. - name: this is a play for testing variables
    3. hosts: dbservers
    4. remote_user: root
    5. vars:
    6. filename: abc.txt
    7. tasks:
    8. - name: position 1
    9. debug:
    10. msg: '测试标签1'
    11. tags:
    12. - one
    13. - name: position 2
    14. debug:
    15. msg: '测试标签2'
    16. tags:
    17. - two
    18. - name: position 3
    19. debug:
    20. msg: '测试标签3'
    21. tags:
    22. - one

     

    6.3 通用标签always的运用  

    1. ---
    2. - name: this is a play for testing variables
    3. hosts: dbservers
    4. remote_user: root
    5. vars:
    6. filename: abc.txt
    7. tasks:
    8. - name: position 1
    9. debug:
    10. msg: '测试标签1'
    11. tags:
    12. - one
    13. - name: position 2
    14. debug:
    15. msg: '测试通用标签always'
    16. tags:
    17. - always
    18. - name: position 3
    19. debug:
    20. msg: '测试标签3'
    21. tags:
    22. - one

     

  • 相关阅读:
    内网渗透代理转发详解及工具介绍
    C 风格的枚举还存在 “成员名称全局有效” 和 “可以隐式转换为整型” 的缺陷
    如何理解相机标定?
    MATLAB中findpeaks函数使用
    数据结构实验教程-第一套
    win11+vs2022配置ceres库
    华清远见11.17
    Python简介-Python3及环境配置
    LotuS2:新一代扩增子数据分析神器(更快、更准、更稳定)
    21.本地存储
  • 原文地址:https://blog.csdn.net/Cnm_147258/article/details/136152944