Ansible-playbook管理复杂任务

一、 playbook

部署环境、搭建服务、修改配置过程中,对于需反复执行的、较为复杂的任务,我们可以用Playbook完成。playbook通过YAML格式进行描述定义,可以实现多台主机应用的部署。

二、yml文件

# cd /etc/ansible
# vim copy.yml

文件内容:

---                           //文档标志符
- hosts: server1     //指定主机
  remote_user: root  //指定在被管理的主机上执行任务的用户
  tasks:      //任务列表
  - name: create user    //任务名
    user: name=wang_06    //调用user模块
  - name: create directory
    command: mkdir /wh_k/wang_06     //调用command模块
  - name: chmod
    command: chown wang_06:wang_06 /wh_k/wang_06  //调用command模块
  - name: copy file
    copy: src=/wh_k/test1.txt dest=/wh_k/wang_06/  //调用copy模块

# ansible-playbook --check copy.yml
# ansible-playbook  copy.yml        

备注

 1.第一行中,文件开头为 ---;这是YAML将文件解释为正确的文档的要求。YAML一个文件支持多个文档,每个“文档”由 --- 符号分割,Ansible只需要一个文件存在一个文档即可。

 2.YAML对空格非常敏感,空格和缩进要注意

 3.任务列表中的各任务按次序逐个在hosts中指定的所有主机上执行,完成第一个任务后再开始第二个。在自上而下运行某playbook时如果中途发生错误,所有该主机上已执行任务都将回滚。

三、示例详解

# vim copy.yml
---                      //文档标志符
- hosts: server1     //定义组和主机
  remote_user: root  // 管理机上执行任务的用户
  gather_facts: false   // 获取主机相关信息 true|false

  vars:           
   - user: "wang_06"  //定义变量

  tasks:
  - name: create user
    command: grep {{user}} /etc/passwd
    ignore_errors: True
    register: result      //查看用户是否存在

  - shell: echo `date +%Y-%m-%d_%H:%M:%S` ":User exit.">>/wh_k/ansible.log
    when: result|succeeded   //用户存在,输出信息

  - user: name="{{user}}"
    when: result|failed          //用户不存在,新建用户

  - name: create directory
    shell: ls /wh_k/{{user}}
    ignore_errors: True
    register: result            //目录是否存在

  - command: mkdir /wh_k/{{user}}     //command可换成shell
    when: result|failed     //目录不存在,新建目录

  - shell: echo `date +%Y-%m-%d_%H:%M:%S`":File is exit,create file failed." >>/wh_k/ansible.log
    when: result|succeeded  //目录存在,输出信息

  - name: chmod
    command: chown {{user}}:{{user}} /wh_k/{{user}}   //赋权

  - name: copy file
    copy: src=/wh_k/test1.txt dest=/wh_k/{{user}}/        //拷贝文件
    notify: insert messages     //当拷贝文件执行正确时,执行调用的操作

  handlers:
  - name: insert messages
    shell: echo `date +%Y-%m-%d_%H:%M:%S`"Ansible  done.">>/wh_k/{{user}}/test1.txt         //定义被调用的操作

说明:

1.gather_facts

gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息  
gather_facts: no 或者是false是关闭,gather_facts:yes 或者是true都是开启

如:

...
gather_facts: true       //开启facts模块
...
- name: test
  shell: touch /tmp/test.txt
  when: facter_ipaddress == "192.168.61.128"    // 主机地址是xxx时,执行操作
        //  facter_ipaddress调用的是facts模块的值,当facts模块开启时,任务才能执行

2.vars

定义变量需要用引号引起来,调用变量需要用大括号引用

如:

vars:
- user: "wang_06"
....
  shell: ls /wh_k/{{user}}

3.检测是否存在

执行相关命令,返回succeeded表示存在,返回false表示不存在

如:

 ...
shell: ls /wh_k/{{user}}
ignore_errors: True
register: result

4.notify

当我们执行 tasks 后,服务器发生变化之后我们要执行一些操作。
上面示例中,只有 copy 模块真正执行后,才会去调用下面的 handlers 相关的操作。
也就是说如果 src 和 dest 内容是一样的,并不会去执行 handlers 里面的 shell 相关命令。
所以这种比较适合配置文件发生更改后,需要重启服务的操作。

5.handlers

定义追加执行的操作,由notify调用。

6.循环执行

某台主机上批量执行任务,比如修改text1.txt 和 test2.txt ...文件属性

如:

 ...
 tasks:
 - name: change mode
    shell: chmod 755 /tmp/{{item}}
    with_items:
     - test1.txt
     - test2.txt
 ...

猜你喜欢

转载自blog.51cto.com/13689359/2333338