ansible之playbook

playbook的核心元素:
  tasks:任务,即调用模块完成的某操作
  variable:变量
  templates:模板
  handlers:处理器,在某条件时触发的动作
  roles:角色
变量形式:
  1、facts,有setup模块输出的信息
  2、--extra-vars “name=value”
  3、roles中定义的变量
  4、主机清单hosts中定义
tasks:下面分为一个个task来执行,针对不同的host来定义不同的tasks
   ansible-playbook:
  ansible-playbook <filename.yml> ... [options]
 
简单实例1:
[root@node1 ansible]# cat task1.yaml
- hosts: nodes
  remote_user: root
  tasks:
  - name: install man
    yum: name=man state=latest
 
实例2:
[root@node1 ansible]# cat httpd.yaml
- hosts: nodes
  remote_user: root
  tasks:
  - name: install httpd service
    yum: name=httpd state=latest
  - name: copy configuration file for nodes
    copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: start httpd service
    service: name=httpd state=started
 
现在将配置文件重新做修改,改为端口为80:
[root@node1 ansible]# ansible-playbook httpd.yaml
 
PLAY [nodes] ***********************************************************************************************************************
 
TASK [Gathering Facts] *************************************************************************************************************
ok: [192.168.223.146]
 
TASK [install httpd service] *******************************************************************************************************
ok: [192.168.223.146]
 
TASK [copy configuration file for nodes] *******************************************************************************************
changed: [192.168.223.146] changed表示这步进行了操作配置文件端口改为了80
 
TASK [start httpd service] *********************************************************************************************************
ok: [192.168.223.146] 然后这里显示ok,并没有changed,也就是说服务并没有重新启动
 
PLAY RECAP *************************************************************************************************************************
192.168.223.146 : ok=4 changed=1 unreachable=0 failed=0
 
观察节点服务:
[root@wadeson ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1190/sshd
tcp 0 0 :::8080 :::* LISTEN 4936/httpd
tcp 0 0 :::22 :::* LISTEN 1190/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 1780/dhclient
 
为了解决这种情况:仅仅是配置文件进行了修改,而服务没有重启
配置文件进行了修改,需要将服务重新启动,这个时候就需要用到handler
配置文件修改后,触发了handler,于是将服务进行了重启的操作
 
将palybook重新修改为:
- hosts: nodes
  remote_user: root
  tasks:
  - name: install httpd service
    yum: name=httpd state=latest
  - name: copy configuration file for nodes
    copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: 这里表示,如果修改了文件就触发notity,然后执行handler
    - restart httpd service 这里定义的名称必须和handler下面的名称一致
  - name: start httpd service
    service: name=httpd state=started
  handlers: handlers和tasks和hosts都是同级目录
  - name: restart httpd service
    service: name=httpd state=restarted
 
[root@node1 ansible]# ansible-playbook httpd.yaml
 
PLAY [nodes] ***********************************************************************************************************************
 
TASK [Gathering Facts] *************************************************************************************************************
ok: [192.168.223.146]
 
TASK [install httpd service] *******************************************************************************************************
ok: [192.168.223.146]
 
TASK [copy configuration file for nodes] *******************************************************************************************
changed: [192.168.223.146] 这里将配置文件端口又修改为了808
 
TASK [start httpd service] *********************************************************************************************************
ok: [192.168.223.146]
 
RUNNING HANDLER [restart httpd service] ********************************************************************************************
changed: [192.168.223.146] 上面配置文件进行修改,于是触发了handler
 
PLAY RECAP *************************************************************************************************************************
192.168.223.146 : ok=5 changed=2 unreachable=0 failed=0
 
在playbook中使用变量:前提已经将节点的httpd服务卸载了
[root@node1 ~]# cat /etc/ansible/httpd_var.yaml
- hosts: nodes
  remote_user: root
  vars: 变量定义
  - package: httpd
  - service_name: httpd
  tasks:
  - name: install httpd service
     yum: name={{ package }} state=latest     这里调用变量格式{{ 变量名 }}
  - name: copy configuration file for nodes
    copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd service
    - name: start httpd service
    service: name={{ service_name }} state=started
  handlers:
  - name: restart httpd service
    service: name=httpd state=restarted
 
facts中的内容,不用定义变量就能使用
ansible all -m setup 显示facts的值
[root@node1 ansible]# cat test_var.yaml
- hosts: nodes
  remote_user: root
  tasks:
  - name: create a new file
    copy: content={{ ansible_hostname }} dest=/tmp/ansible_var.txt
调用facts的值,copy模块创建一个有内容的文件
 
在hosts清单文件中定义变量,并进行调用:
[root@node1 ansible]# cat hosts
[nodes]
192.168.223.146 node_var=192.168.223.146 然后yaml中调用这个变量(为组中的主机单独定义)
[root@node1 ansible]# cat test_var.yaml
- hosts: nodes
  remote_user: root
  tasks:
  - name: create a new file
    copy: content={{ node_var }} dest=/tmp/ansible_var.txt
 
在playbook中使用条件判断:
[root@node1 ansible]# cat test_when.yaml
- hosts: nodes 因为nodes这个组只有wadeson这个hostname的节点
  remote_user: root
  tasks:
  - name: create a user
    user: name=json_hc state=present
    when: ansible_hostname != "wadeson" 当节点中hostname=wadeson的这个节点才不创建该用户
 
当有需要重复执行的任务时,可以使用迭代机制,item是固定使用变量名,并通过with_items语句
来指明迭代的元素列表即可
实例:
[root@node1 ansible]# cat test_item.yaml
- hosts: nodes
  remote_user: root
  tasks:
  - name: create several users
    user: name={{ item }} state=present item固定变量名
    with_items: 下面列表具体变量值
    - testuser1
    - testuser2
 
也可以使用下面方法:
 when结合items使用:

[root@node1 ansible]# cat tar.yaml
- hosts: nodes
  remote_user: root
  vars:
  - sysnum: ls -l /root/tools/scripts/apr-1.6.2.tar.gz |wc -l                      这里变量sysnum的值为1
  tasks:
  - name: tar
    shell: cd /root/tools/scripts; tar xf apr-1.6.2.tar.gz
    when: item != 1         当条件为sysnum值不等于1的时候,才执行上面的shell任务解压该包
    with_items:
    - "{{ sysnum }}"
[root@node1 ansible]# ansible-playbook tar.yaml

PLAY [nodes] ***********************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [192.168.223.146]

TASK [tar] *************************************************************************************************************************
changed: [192.168.223.146] => (item=ls -l /root/tools/scripts/apr-1.6.2.tar.gz |wc -l)

PLAY RECAP *************************************************************************************************************************
192.168.223.146 : ok=2 changed=1 unreachable=0 failed=0

这里只是介绍when结合items使用,调用前面的变量进行判断

原文地址:https://www.cnblogs.com/jsonhc/p/7263815.html