Ansible Loop循环 循环遍历的属性 Notify和Handlers

Loop循环

loop:循环属于当前任务的一个功能,归属于-name下面

循环中,提供一个个数据的项,每一项都要由- 定义

它会一项项重复执行当前的任务,每执行一次就会提取一项值,交给变量{{ item }}

注意:当前循环只对当前的任务有效的,任务执行的时相同功能

复制代码
[root@ansible ~]# vi an-9.yml 
[root@ansible ~]# cat an-9.yml
- hosts: webservers
  tasks:
    - name: 循环创建文件
      shell: "touch {{ item }}"
      args:
        chdir: /opt
      loop:
        - zhangsan.txt
        - lisi.txt
        - wangwu.txt
        - xiyangyang.txt 
[root@ansible ~]# ansible-playbook an-9.yml

PLAY [webservers] ***************************************************************************************************************************************

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

TASK [循环创建文件] *************************************************************************************************************************************
changed: [192.168.92.20] => (item=zhangsan.txt)
changed: [192.168.92.20] => (item=lisi.txt)
changed: [192.168.92.20] => (item=wangwu.txt)
changed: [192.168.92.20] => (item=xiyangyang.txt)

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

循环遍历的属性

复制代码
[root@ansible ~]# vi an-10.yml
[root@ansible ~]# cat an-10.yml
- hosts: webservers
  tasks:
    - name: 创建用户组
      group:
        name: "{{ item }}"
        state: present
      loop:
        - gp1
        - gp2
        - gp3

    - name: 创建用户并设置基本组
      user:
        name: "{{ item.uname }}"
        group: "{{ item.group }}"
        state: present
      loop:
        - { uname: test1, group: gp1 }
        - { uname: test2, group: gp2 }
        - { uname: test3, group: gp3 }
[root@ansible ~]# ansible-playbook an-10.yml

PLAY [webservers] ***************************************************************************************************************************************

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

TASK [创建用户组] ***************************************************************************************************************************************
changed: [192.168.92.20] => (item=gp1)
changed: [192.168.92.20] => (item=gp2)
changed: [192.168.92.20] => (item=gp3)

TASK [创建用户并设置基本组] *****************************************************************************************************************************
changed: [192.168.92.20] => (item={'uname': 'test1', 'group': 'gp1'})
changed: [192.168.92.20] => (item={'uname': 'test2', 'group': 'gp2'})
changed: [192.168.92.20] => (item={'uname': 'test3', 'group': 'gp3'})

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ansible ~]#

Notify和Handlers

复制代码
[root@ansible ~]# vi an-11.yml 
[root@ansible ~]# cat an-11.yml
# handlers 是一个触发器,同时也是 tasks 任务(不会直接运行,需要被触发)
# notify 就是通知

# 安装 httpd 服务器,然后下发更改后的配置文件,当被控端发生改变时就重启服务
- hosts: webservers
  tasks:
    - name: 安装 httpd
      yum:
        name: httpd
        state: latest

    - name: 启动 httpd 服务
      service:
        name: httpd
        state: started
[root@ansible ~]# ansible-playbook an-11.yml

PLAY [webservers] ***************************************************************************************************************************************

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

TASK [安装 httpd] ***************************************************************************************************************************************
ok: [192.168.92.20]

TASK [启动 httpd 服务] **********************************************************************************************************************************
ok: [192.168.92.20]

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
[root@web20h ~]#  scp /etc/httpd/conf/httpd.conf root@192.168.92.19:/root
The authenticity of host '192.168.92.19 (192.168.92.19)' can't be established.
ED25519 key fingerprint is SHA256:TtJq+VxSoGlLTkPvOJuAU+TJ+MZsYo6TuFHufmCOTeI.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.92.19' (ED25519) to the list of known hosts.
root@192.168.92.19's password:
httpd.conf       
[root@ansible ~]# ls
3.26.sh  an-1.yml   an-11.yml  an-3.yml  an-5.yml  an-7.yml  an-9.yml         bianliang.yml  cp.txt      host_vars   replace.txt
a.txt    an-10.yml  an-2.yml   an-4.yml  an-6.yml  an-8.yml  anaconda-ks.cfg  cp.tar.gz      group_vars  httpd.conf
[root@ansible ~]# vi httpd.conf 
Listen 66 #改了一下监听端口 
[root@ansible ~]# vi an-11.yml 
[root@ansible ~]# cat an-11.yml
# handlers 是一个触发器,同时也是 tasks 任务(不会直接运行,需要被触发)
# Notify 就是通知

# 安装 httpd 服务器,然后下发更改后的配置文件,当被控端发生改变时就重启服务
- hosts: webservers
  tasks:
    - name: 安装 httpd
      yum:
        name: httpd
        state: latest
    - name: 启动 httpd 服务
      service:
        name: httpd
        state: started
    - name: 下发配置文件
      copy:
        src: /root/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        mode: '664'
      notify: restart httpd
  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted 
[root@ansible ~]# ansible-playbook an-11.yml

PLAY [webservers] ***************************************************************************************************************************************

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

TASK [安装 httpd] ***************************************************************************************************************************************
ok: [192.168.92.20]

TASK [启动 httpd 服务] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [下发配置文件] *************************************************************************************************************************************
changed: [192.168.92.20]

RUNNING HANDLER [restart httpd] *************************************************************************************************************************
changed: [192.168.92.20]

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0                                                                                                      100%   12KB   7.7MB/s   00:00
相关推荐
一直会游泳的小猫7 小时前
homebrew
linux·mac·工具·包管理
Agent产品评测局7 小时前
制造业生产调度自动化落地,完整步骤与避坑指南:2026企业级智能体选型与实战全景
运维·人工智能·ai·chatgpt·自动化
寒秋花开曾相惜7 小时前
(学习笔记)4.2 逻辑设计和硬件控制语言HCL(4.2.1 逻辑门&4.2.2 组合电路和HCL布尔表达式)
linux·网络·数据结构·笔记·学习·fpga开发
狂奔的sherry7 小时前
一次由 mount 引发的 Linux 文件系统“错觉”
linux·运维·服务器
志栋智能8 小时前
超自动化巡检:让合规与审计变得轻松简单
运维·网络·人工智能·自动化
小黑要努力8 小时前
智能音箱遇到的问题(一)
linux·运维·git
好度8 小时前
自动化教程-封装浏览器驱动
运维·自动化
ch3nyuyu8 小时前
静态库和动态库的制作
linux·运维·开发语言
程序员老邢8 小时前
【产品底稿 07】商助慧 Admin 运维模块落地:从 “能跑” 到 “能运维”,3 个页面搞定日常排障
java·运维·经验分享·spring boot·后端
一口Linux9 小时前
Linux C编程 | 从0实现telnet获取程序终端控制权
linux·运维·c语言