详解ansible批量管理服务(3)

运行剧本

root@m01 ansible_playbook]#ansible-playbook -C test_判断功能.yaml 

PLAY [nfs_client] ************************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]
ok: [172.16.1.7]

TASK [create file for 41 host] ***********************************************************************
skipping: [172.16.1.7]
changed: [172.16.1.41]

TASK [create file for 7 host] ************************************************************************
skipping: [172.16.1.41]
changed: [172.16.1.7]

PLAY RECAP *******************************************************************************************
172.16.1.41    : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 
172.16.1.7     : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 

1.6.4 剧本信息循环功能

循环创建多个用户

[root@m01 ansible_playbook]#vim test_循环功能.yaml
- hosts: 172.16.1.41
 tasks:
 - name: create user
  user: name={{ item }}
  with_items:
  - oldgirl01
  - oldgirl02
  - oldgirl03
  - oldgirl04
  - oldgirl05

循环创建多个用户 多个用户uid数值是不同的

[root@m01 ansible_playbook]#vim test_循环功能.yaml
- hosts: 172.16.1.41
 tasks:
 - name: create user
  user: name={{ item.name }} uid={{ item.uid }}
  with_items:
  - {name: "oldgirl06", uid: "3006"}
  - {name: "oldgirl07", uid: "3007"}
  - {name: "oldgirl08", uid: "3008"}
  - {name: "oldgirl09", uid: "3009"}
  - name: check create user info
   shell: grep oldgirl0 /etc/passwd
   register: user_info
  - debug: msg={{ user_info.stdout_lines }}

1.6.5 剧本编写忽略错误功能

忽略功能主要用来调试剧本

[root@m01 ansible_playbook]#vim test_h忽略功能.yaml
- hosts: 172.16.1.41
 tasks:
 - name: create rsync user
  shell: useradd rsync -M -s /sbin/nologin
  ignore_errors: yes
 - name: create backup dir
  shell: mkdir /backup
  ignore_errors: yes
 - name: boot server
  shell: systemctl start rsyncd
  ignore_errors: yes

在使用shell进行一些操作时,shell产生的结果已经存在时,会导致剧本无法进行下去,因此使用忽略功能可以有效的使剧本进行下去。

1.6.6 剧本标签设置功能

标签功能主要用来调试剧本

tags:标签

[root@m01 ansible_playbook]#vim test_标签功能.yaml
- hosts: 172.16.1.41
 tasks:
 - name: 01:安装软件
  yum: name=rsync state=installed
  ignore_errors: yes
 - name: 02:创建用户
  user: name=rsync create_home=no shell=/sbin/nologin
  ignore_errors: yes
  tags: create_user
 - name: 03:创建目录
  file: path=/backup state=directory