Ansible之Playbook(二)

简介: Ansible之Playbook(二)

-Playbook中变量的使用


-首先先定义主机组

[root@ansible ~]# tail -7 /etc/ansible/hosts 
[web01]
192.168.100.203
[web02]
192.168.100.204
[web:children]
web01
web02

命令行执行变量

#执行剧本时可以通过-e参数传入变量,这样传入的变量在目标剧本中都可以被调用,属于全局变量,并且这样定义的变量优先级是最高的
[root@ansible ~]# vim playbook02.yaml
---
- hosts: web01
  remote_user: root
  tasks:
  - name: 创建新文件
    file: name={{ hehe }} state=touch #这里定义变量hehe
#保存退出
[root@ansible ~]# ansible-playbook -e "hehe=aaaaaa" playbook02.yaml   #使用-e导入变量hehe,并且执行剧本,要注意这样导入变量的剧本,在使用-C监测是会报错的
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建新文件] ****************************************************************************************************************************
changed: [192.168.100.203]
TASK [写入数据] *****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible web01 -m shell -a 'ls'  #验证效果
192.168.100.203 | SUCCESS | rc=0 >>
aaaaaa
anaconda-ks.cfg

-hosts文件中自定义变量


#在ansible的hosts文件中可以自定义变量,定义的是主机或者一个组的变量,要注意的是,一个组定义的变量的优先级没有单个主机定义变量的优先级高
[root@ansible ~]# vim /etc/ansible/hosts     #分别给主机和组定义变量测测试效果
[web01]
192.168.100.203 hehe=bbbbbb    
[web02]
192.168.100.204
[web01:vars]
hehe=aaaaaa
[root@ansible ~]# ansible web01 -m shell -a 'rm -rf aaaaaa'  #删除aaaaaa
 [WARNING]: Consider using the file module with state=absent rather than running rm.  If you need to use command because file is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
192.168.100.203 | SUCCESS | rc=0 >>
[root@ansible ~]# ansible-playbook -C playbook02.yaml  #监测剧本
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建新文件] ****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook  playbook02.yaml   #执行剧本
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建新文件] ****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web01 -m shell -a 'ls'  #验证效果,发现是主机定义的变量生效了
192.168.100.203 | SUCCESS | rc=0 >>
anaconda-ks.cfg
bbbbbb

-剧本文件中定义变量

#在编写剧本时,可以直接在剧本中定义变量,然后直接引用,可以一次性定义多个变量,注意剧本里定义的变量的优先级是没有通过-e参数定义变量的优先级高的
[root@ansible ~]# vim playbook02.yaml   #修改剧本文件
---
- hosts: web01
  remote _user: root
  vars:         #定义变量
    hehe: abcde
  tasks:
  - name: 创建新文件
    file: name={{ hehe }} state=touch
[root@ansible ~]# ansible web01 -m shell -a 'rm -rf bbbbbb'  #删除之前创建的文件
[WARNING]: Consider using the file module with state=absent rather than running rm.  If you need to use command because file is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
192.168.100.203 | SUCCESS | rc=0 >>
[root@ansible ~]# ansible-playbook -C playbook02.yaml   #监测剧本是否有问题
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建新文件] ****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook playbook02.yaml   #执行剧本
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建新文件] ****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web01 -m shell -a 'ls'  #验证效果
192.168.100.203 | SUCCESS | rc=0 >>
abcde
anaconda-ks.cfg
[root@ansible ~]# ansible web01 -m shell -a 'rm -rf abcde'  #删除abcde
 [WARNING]: Consider using the file module with state=absent rather than running rm.  If you need to use command because file is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
192.168.100.203 | SUCCESS | rc=0 >>
[root@ansible ~]# ansible-playbook -e "hehe=aaaaaa" playbook02.yaml   #使用-e参数执行剧本
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建新文件] ****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web01 -m shell -a 'ls'   #验证效果,发现是-e参数定义的变量生效
192.168.100.203 | SUCCESS | rc=0 >>
aaaaaa
anaconda-ks.cfg

-调用setup模块获取变量

#setup模块默认是获取主机信息的,有时候在剧本中需要使用,所以可以直接调用,其实就是内置变量
[root@ansible ~]# vim playbook03.yaml
---
- hosts: web01
  remote_user: root
  tasks:
  - name: 创建文件
    file: name={{ ansible_fqdn }}.log state=touch   #引用内置变量ansible_fqdn,这个内置变量的作用是获取主机名
#保存退出
[root@ansible ~]# ansible-playbook -C playbook03.yaml   
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook  playbook03.yaml   #执行剧本
PLAY [web01] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.203]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.203]
PLAY RECAP ******************************************************************************************************************************
192.168.100.203            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web01 -m shell -a 'ls'
192.168.100.203 | SUCCESS | rc=0 >>
aaaaaa
anaconda-ks.cfg
node1.log   #这就是剧本创建的文件

-独立的变量——yaml文件中定义


#为了方便管理可以将所有的变量统一放在一个独立的变量yaml文件中,剧本文件直接引用变量yaml文件即可
[root@ansible ~]# vim var.yml    #创建存放变量的yml文件
aaa: hehe
bbb: haha
#保存退出
[root@ansible ~]# vim text.yaml  #创建剧本
---
- hosts: web02
  remote_user: root
  vars_files:
    - /root/var.yml
  tasks:
  - name: 创建文件
    file: path=/root/{{ aaa }}.txt state=touch   #分别引用变量
  - name: 创建文件
    file: path=/root/{{ bbb }}.txt state=touch
#保存退出
[root@ansible ~]# ansible-playbook -C text.yaml   #监测剧本是否有问题
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook text.yaml   #执行剧本
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'ls'  #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
anaconda-ks.cfg
haha.txt
hehe.txt 

-Playbook中标签的使用

#如果想只执行剧本中的单独一个任务,那么只需要给每个任务集添加标签,这样在想执行单个任务时,直接通过-t参数引用标签来执行对应的命令,还可以通过--skip-tags选择除了某个标签其他的标签全部执行
[root@ansible ~]# vim tags.yaml
---
- hosts: web02
  remote_user: root
  tasks:
  - name: 创建aaa文件
    file: path=/root/aaa state=touch
    tags: touch
  - name: 创建bbb目录
    file: path=/root/bbb state=directory
    tage: directory
  - name: 创建用户ccc
    user: name=ccc 
    tage: user
#保存退出
[root@ansible ~]# ansible web02 -m shell -a 'rm -rf *'  #先删除web02的root下的所有文件
 [WARNING]: Consider using the file module with state=absent rather than running rm.  If you need to use command because file is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
192.168.100.204 | SUCCESS | rc=0 >>
[root@ansible ~]# ansible web02 -m shell -a 'ls'  #确认删除
192.168.100.204 | SUCCESS | rc=0 >>
 [root@ansible ~]# ansible-playbook -C tags.yaml  #监测剧本是否有问题
 [WARNING]: Ignoring invalid attribute: tage
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建aaa文件] **************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建bbb目录] **************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建用户ccc] **************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=4    changed=3    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook  tags.yaml   #先正常执行剧本,查看效果
 [WARNING]: Ignoring invalid attribute: tage
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建aaa文件] **************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建bbb目录] **************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建用户ccc] **************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=4    changed=3    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'ls'   #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
aaa
bbb
[root@ansible ~]# ansible web02 -m shell -a 'tail -1 /etc/passwd'   #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
ccc:x:1000:1000::/home/ccc:/bin/bash
[root@ansible ~]# ansible web02 -m shell -a 'rm -rf *'  #删除创建的文件
 [WARNING]: Consider using the file module with state=absent rather than running rm.  If you need to use command because file is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
192.168.100.204 | SUCCESS | rc=0 >>
[root@ansible ~]# ansible web02 -m user -a 'name=ccc state=absent'  #删除用户
192.168.100.204 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "ccc", 
    "remove": false, 
    "state": "absent"
} 
[root@ansible ~]# ansible-playbook -t touch tags.yaml   #只执行标签为touch的命令
 [WARNING]: Ignoring invalid attribute: tage
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建aaa文件] **************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0     #可以看到只执行了一条命令
[root@ansible ~]# ansible web02 -m shell -a 'ls'  #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
aaa
[root@ansible ~]# ansible-playbook --skip-tags  touch tags.yaml  #除了标签为touch的命令其他的命令都执行
 [WARNING]: Ignoring invalid attribute: tage
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建bbb目录] **************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建用户ccc] **************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0     #可以看到三条命令执行了两条命令
[root@ansible ~]# ansible web02 -m shell -a 'ls'  #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
bbb
[root@ansible ~]# ansible web02 -m shell -a 'tail -1 /etc/passwd'  #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
ccc:x:1000:1000::/home/ccc:/bin/bash

-Playbook中模板的使用(.j2)


  • template模板提供了动态配置服务,使用jinja2语言,支持多种条件判断、循环、逻辑运算、比较操作等,其实也是一个文件,和之前配置文件使用copy一样,只是使用copy,不能根据服务器配置不一样而进行不同的动态配置,这样不利于管理
  • 大多数情况下都将template文件放在和剧本文件同级的templates目录下,需要手动创建,这样剧本文件可以直接引用,会自动去找这个文件,.j2模板文件可以应用playbook剧本中的变量,只需要在模板文件中使用{{ }}去定义变量名称,然后在剧本中就可以指定变量值
  • 模板文件后缀名为.j2
#循环参考
[root@ansible ~]# vim text.yaml
---
- hosts: web02
  remote_user: root
  vars:
    - listen_port: 8080
  tasks:
  - name: 安装httpd
    yum: name=httpd state=installed
  - name: 修改httpd配置文件
    template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf  #使用模板
    notify:    #使用触发
      - restart httpd
  - name: 启动httpd
    service: name=httpd state=started
  handlers:  #配置触发相应的动作
    - name: restart httpd
      service: name=httpd state=restarted
 #保存退出
 [root@ansible ~]# ll
总用量 16
-rw-r--r-- 1 root root 11753 6月  23 22:49 httpd.conf   #准备一个配置文件
-rw-r--r-- 1 root root   417 6月  23 22:47 text.yaml
[root@ansible ~]# vim httpd.conf
。。。。。。。
 41 #Listen 12.34.56.78:80
 42 Listen {{ listen_port }}   #引用变量
 43 
。。。。。。
#保存退出
[root@ansible ~]# mv httpd.conf httpd.conf.j2  #修改名称变成jinja2模板文件
[root@ansible ~]# mkdir templates
[root@ansible ~]# mv httpd.conf.j2 templates/
[root@ansible ~]# yum -y install tree
。。。。。。
完毕!
[root@ansible ~]# tree
.
├── templates              #templates需要和剧本文件是同级关系,然后再templates中存放要copy的.j2模板文件
│   └── httpd.conf.j2
└── text.yaml
[root@ansible ~]# ansible-playbook -C text.yaml   #监测剧本是否有问题
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [安装httpd] **************************************************************************************************************************
changed: [192.168.100.204]
TASK [修改httpd配置文件] **********************************************************************************************************************
changed: [192.168.100.204]
TASK [启动httpd] **************************************************************************************************************************
changed: [192.168.100.204]
RUNNING HANDLER [restart httpd] *********************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=5    changed=4    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'echo "aaaa" > /var/www/html/index.html'  #编写网页
192.168.100.204 | SUCCESS | rc=0 >>
[root@ansible ~]# curl 192.168.100.204:8080  #测试验证效果
aaaa


-template之when


  • when是条件测试,如果需要根据变量、facts或者此前任务的执行结果来决定某个tasks是否执行的话,可以使用when进行条件判断
  • when语句一般在tasks后添加即可进行条件测试
  • when语句支持jinja2表达式语法
[root@ansible ~]# vim when.yaml 
---
- hosts: web02
  remote_user: root
  tasks:
  - name: 创建文件
    file: path=/root/aaa state=touch
    when: ansible_distribution_major_version == "7"     #当系统版本等于7时才会执行对应的命令并且触发动作
    notify:
    - echo01
  - name: 创建文件
    file: path=/root/bbb state=touch
    when: ansible_distribution_major_version == "6"     #当系统版本等于6时才会执行对应的命令并且触发动作
    notify:
    - echo02
  handlers:   #动作
    - name: echo01
      shell: echo "777777777777" > /root/aaa
    - name: echo02
      shell: echo "666666666666" > /root/bbb
#保存退出
[root@ansible ~]# ansible-playbook -C when.yaml   #监测剧本是否有问题
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
skipping: [192.168.100.204]
RUNNING HANDLER [echo01] ****************************************************************************************************************
skipping: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook   when.yaml   #执行剧本
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
skipping: [192.168.100.204]
RUNNING HANDLER [echo01] ****************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'cat aaa'  #验证效果,因为系统版本是7,所以触发了echo01动作
192.168.100.204 | SUCCESS | rc=0 >>
777777777777

-template之with_items


  • with_items迭代,当有需要重复性执行的任务时,可以使用迭代机制,一个任务下的items的迭代变量只会在当前任务下生效
  • 对迭代项的引用,固定变量名为“item”,要在task中使用
  • with_items定义要迭代的元素列表,这个列表可以是字符串和字典
******迭代写入文件内容
[root@ansible ~]# vim items.yaml
---
- hosts: web02
  remote_user: root
  tasks:
  - name: 创建文件
    file: path=/root/abc state=touch
  - name: 写入内容
    shell: echo "{{ item }}" >> /root/abc
    with_items:            #迭代执行,类似于for循环,item的值会从123一直到ABC
      - 123
      - abc
      - ABC
#保存退出      
————————————————————————————————————————
#上面的写法相当于
 - name: 写入内容
    shell: echo "123" >> /root/abc
  - name: 写入内容
    shell: echo "abc" >> /root/abc
   - name: 写入内容
    shell: echo "ABC" >> /root/abc  
————————————————————————————————————————
[root@ansible ~]# ansible-playbook -C items.yaml   #监测剧本是否有问题
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
TASK [写入内容] *****************************************************************************************************************************
skipping: [192.168.100.204] => (item=123) 
skipping: [192.168.100.204] => (item=abc) 
skipping: [192.168.100.204] => (item=ABC) 
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook  items.yaml  #执行剧本
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建文件] *****************************************************************************************************************************
changed: [192.168.100.204]
TASK [写入内容] *****************************************************************************************************************************
changed: [192.168.100.204] => (item=123)
changed: [192.168.100.204] => (item=abc)
changed: [192.168.100.204] => (item=ABC)
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'cat /root/abc'  #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
123
abc
ABC
******迭代创建用户,并且迭代指定用户名和组
[root@ansible ~]# vim users.yaml
---
- hosts: web02
  remote_user: root
  tasks:
  - name: 创建新组
    group: name={{ item }} state=present
    with_items:     #items可以是字符串
      - B1
      - B2
      - B3
  - name: 创建用户
    user: name={{ item.name }} group={{ item.group }} state=present
    with_items:     #items可以是字典类型的,值只需要用.指定即可
      - { name: 'A1', group: 'B1' }   #注意空格隔开
      - { name: 'A2', group: 'B2' }
      - { name: 'A3', group: 'B3' }
#保存退出
[root@ansible ~]# ansible-playbook -C users.yaml   #监测剧本是否有问题
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建新组] *****************************************************************************************************************************
changed: [192.168.100.204] => (item=B1)
changed: [192.168.100.204] => (item=B2)
changed: [192.168.100.204] => (item=B3)
TASK [创建用户] *****************************************************************************************************************************
changed: [192.168.100.204] => (item={u'group': u'B1', u'name': u'A1'})
changed: [192.168.100.204] => (item={u'group': u'B2', u'name': u'A2'})
changed: [192.168.100.204] => (item={u'group': u'B3', u'name': u'A3'})
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook users.yaml  #执行剧本
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [创建新组] *****************************************************************************************************************************
changed: [192.168.100.204] => (item=B1)
changed: [192.168.100.204] => (item=B2)
changed: [192.168.100.204] => (item=B3)
TASK [创建用户] *****************************************************************************************************************************
changed: [192.168.100.204] => (item={u'group': u'B1', u'name': u'A1'})
changed: [192.168.100.204] => (item={u'group': u'B2', u'name': u'A2'})
changed: [192.168.100.204] => (item={u'group': u'B3', u'name': u'A3'})
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=3    changed=2    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a "tail -3 /etc/passwd"  #验证效果,是否成功创建用户
192.168.100.204 | SUCCESS | rc=0 >>
A1:x:1004:1007::/home/A1:/bin/bash
A2:x:1005:1008::/home/A2:/bin/bash
A3:x:1006:1009::/home/A3:/bin/bash
[root@ansible ~]# vim users.yaml
[root@ansible ~]# ansible web02 -m shell -a "tail -3 /etc/group"   #是否成功创建组
192.168.100.204 | SUCCESS | rc=0 >>
B1:x:1007:
B2:x:1008:
B3:x:1009:
[root@ansible ~]# ansible web02 -m shell -a "groups A1"  #是否成功把用户分配到相应的组中
192.168.100.204 | SUCCESS | rc=0 >>
A1 : B1
[root@ansible ~]# ansible web02 -m shell -a "groups A2"
192.168.100.204 | SUCCESS | rc=0 >>
A2 : B2
[root@ansible ~]# ansible web02 -m shell -a "groups A3"
192.168.100.204 | SUCCESS | rc=0 >>
A3 : B3


-template之for、 if


通过使用for、if可以更加灵活的生成配置文件等需求,还可以在里面根据各种条件进行判断,然后生成不同的配置文件、或者服务器配置相关等


通常是使用在批量配置nginx等web服务的虚拟主机时使用

******简单的使用for循环配合模板编写文件内容
[root@ansible ~]# vim text.yaml
---
- hosts: web02
  remote_user: root
  vars:
    port:     #定义变量
      - 80
      - 81
      - 82
  tasks:
  - name: 复制模板文件
    template: src=/root/abc.j2 dest=/root/abc.txt  
#保存退出
[root@ansible ~]# vim abc.j2  #编写模板文件
{% for i in port %}   #使用for循环进行取值
------------------------
nginx端口号是:{{ i }}
------------------------
{% endfor %}
#保存退出
[root@ansible ~]# ansible-playbook -C text.yaml   #监测剧本是否有问题
 [WARNING]: Found variable using reserved name: port
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook text.yaml   #执行剧本
 [WARNING]: Found variable using reserved name: port
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'cat abc.txt'  #验证效果,可以应用到nginx创建虚拟主机中
192.168.100.204 | SUCCESS | rc=0 >>
------------------------
nginx端口号是:80
------------------------
------------------------
nginx端口号是:81
------------------------
------------------------
nginx端口号是:82
------------------------
******继续上面的剧本,使用多层嵌套的for循环
[root@ansible ~]# vim text.yaml 
---
- hosts: web02
  remote_user: root
  vars:
    text:      #使用多层嵌套来定义变量
      - A:
        port: 80
        name: aaa
        age: 18
      - B:
        port: 81
        name: bbb
        age: 19
      - C:
        port: 82
        name: ccc
        age: 20
  tasks:
  - name: 复制模板文件
    template: src=/root/vvv.j2 dest=/root/vvv.txt
#保存退出
[root@ansible ~]# vim vvv.j2
{% for i in text %}
--------------------------
端口号是:{{ i.port }}
主机名是:{{ i.name }}
存活时间是: {{ i.age }}
--------------------------
{% endfor %}
#保存退出
[root@ansible ~]# ansible-playbook -C text.yaml  #监测剧本是否有问题,当模板有问题时也会报错
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook  text.yaml   #执行剧本
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'cat vvv.txt'   #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
--------------------------
端口号是:80
主机名是:aaa
存活时间是: 18
--------------------------
--------------------------
端口号是:81
主机名是:bbb
存活时间是: 19
--------------------------
--------------------------
端口号是:82
主机名是:ccc
存活时间是: 20
--------------------------
******在for循环中再嵌套if语句,可以使生成的数据更加灵活
[root@ansible ~]# vim text.yaml
---
- hosts: web02
  remote_user: root
  vars:
    text:
    - A:
      port: 80
      name: aaa
      age: 18
    - B:        #留一个不设置port变量的
      name: bbb
      age: 19
    - C:
      port: 82
      name: ccc
      age: 20
  tasks:
  - name: 复制模板文件
    template: src=/root/rrr.j2 dest=/root/rrr.txt
#保存退出   
[root@ansible ~]# vim rrr.j2
{% for i in text %}
信息:
{% if i.port is defined %}  #这里的is defined表示当i.port变量语句定义的时候就输出端口号: {{ i.port }}
端口号: {{ i.port }}
{% else %}   #否则输出端口号: 8888
端口号: 8888
{% endif %}  #第一个if语句结束
主机名: {{ i.name }}
存活时间: {{ i.age }}
{% endfor %}
#保存退出
[root@ansible ~]# ansible-playbook -C text.yaml 
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook -C text.yaml 
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible-playbook  text.yaml  #执行剧本
PLAY [web02] ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.100.204]
TASK [复制模板文件] ***************************************************************************************************************************
changed: [192.168.100.204]
PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=2    changed=1    unreachable=0    failed=0   
[root@ansible ~]# ansible web02 -m shell -a 'cat rrr.txt' #验证效果
192.168.100.204 | SUCCESS | rc=0 >>
信息:
端口号: 80
主机名: aaa
存活时间: 18 
信息:
端口号: 8888  #因为在剧本中第二个是没有定义变量值的,所以通过if语句,这里输出的是8888
主机名: bbb
存活时间: 19 
信息:
端口号: 82
主机名: ccc
存活时间: 20 


目录
相关文章
|
7月前
Ansible之playbook剧本-1
Ansible之playbook剧本
88 0
|
7月前
Ansible之playbook剧本-3
Ansible之playbook剧本
47 0
|
7月前
|
Python
Ansible之playbook剧本-2
Ansible之playbook剧本
33 0
|
8月前
|
运维 关系型数据库 应用服务中间件
【运维知识进阶篇】Ansible自动化运维-PlayBook详解
【运维知识进阶篇】Ansible自动化运维-PlayBook详解
125 0
|
9月前
|
应用服务中间件 Linux 数据安全/隐私保护
ansible:playbook学习笔记
ansible:playbook学习笔记
98 0
|
10月前
|
Shell 网络安全 开发工具
|
11月前
|
运维 监控 应用服务中间件
自动化运维工具-Ansible的Playbook的使用
自动化运维工具-Ansible的Playbook的使用
169 0
|
Shell Linux Python
Ansible之Playbook(一)
Ansible之Playbook
90 0
Ansible之Playbook(一)
|
JSON 运维 Kubernetes
[Ansible专栏]Ansible Playbook介绍和使用
[Ansible专栏]Ansible Playbook介绍和使用
|
XML NoSQL 关系型数据库
实现 Ansible 企业级用法 playbook| 学习笔记
快速学习实现 Ansible 企业级用法 playbook
185 0
实现 Ansible 企业级用法 playbook| 学习笔记