码迷,mamicode.com
首页 > 其他好文 > 详细

9.ansible 循环功能和忽略错误

时间:2021-03-31 11:52:29      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:errors   use   pat   lse   man   install   log   command   剧本   

在剧本中设置循环信息
vim test04.yml

  • hosts: all
    remote_user: root
    tasks:
    • name: Add Users
      user: name={{ item.name }} groups={{ item.groups }} state=present
      with_items:
      - { name: ‘testuser1‘, groups: ‘bin‘ }
      - { name: ‘testuser2‘, groups: ‘root‘ }

vim test05.yml

  • hosts: all
    remote_user: root
    tasks:
    • name: Installed Pkg
      yum: name={{ item }} state=present
      with_items:
      - wget
      - tree
      - lrzsz

实践配置:
[root@m01 ansible_playbook]# cat test_循环功能.yaml

  • hosts: 172.16.1.7
    tasks:

    - name: 创建多个用户

    user: name={{ item.name }} shell={{ item.shell }} create_home={{ item.create_home }}

    with_items:

    - { name: ‘oldboy01‘, shell: ‘/sbin/nologin‘, create_home: ‘no‘ }

    - { name: ‘oldboy02‘, shell: ‘/sbin/nologin‘, create_home: ‘yes‘ }

    - { name: ‘oldboy03‘, shell: ‘/bin/bash‘, create_home: ‘yes‘ }

    • name: 创建多个用户
      user: name={{ item }} shell=/sbin/nologin create_home=no
      with_items:
      • oldboy04
      • oldboy05
      • oldboy06

扩展说明:单独加载指定主机清单文件
ansible-playbook nfs_auto.yaml -i ./nfs_hosts
-i 指定加载主机清单
在剧本中设置忽略错误
默认playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行
可以加入ignore_errors: yes忽略错误
vim test06.yml
- hosts: all
remote_user: root
tasks:
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file: path=/tmp/oldboy_ignore state=touch

实践配置: 
[root@m01 ansible_playbook]# cat test_忽略错误.yaml 
- hosts: 172.16.1.7
  tasks:
    - name: create user
      shell: useradd oldboy01 -s /sbin/nologin -M
      ignore_errors: yes
    - name: create dir
      shell: mkdir /oldboy
      ignore_errors: yes
    - name: create file
      shell: touch /etc/oldboy.txt

9.ansible 循环功能和忽略错误

标签:errors   use   pat   lse   man   install   log   command   剧本   

原文地址:https://www.cnblogs.com/yangtao416/p/14595934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!