39 ansible 列表和字典

本文介绍了如何在Ansible中使用列表和字典,包括条件判断、循环操作,如for循环,并展示了列表及字典在配置文件中的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[root@node1 ansibletest]# cat testtemplate.yaml 
   - hosts: all
     remote_user: root
   
     tasks:
     - name: install nginx
       yum: name=nginx
     - name: copy temp
       template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
     - name: start service
       service: name=nginx state=started
修改模板
 cat  templates/nginx.conf.j2 | grep process
worker_processes {{ ansible_processor_vcpus**2 }};
 cat  templates/nginx.conf.j2 | grep port
        listen       {{ http_port }} default_server;

模板

[root@node1 ansibletest]# cat testtemplate.yaml 
- hosts: all
  remote_user: root
 
  tasks:
  - name: install nginx
    yum: name=nginx
  - name: copy temp
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
    notify: restart service
  - name: start service
    service: name=nginx state=started

  handlers:
  - name: restart service
    service: name=nginx state=restarted

条件判断

​ [根据facts的变量来进行判断]

​ when 在task中使用

tasks:
 - name: conf
   template: src=x dest=y
   when: ansible_distribution_major_version == "7"
[root@node1 ansibletest]# ansible all -m setup | grep version
        "ansible_distribution_major_version": "7", 
        "ansible_distribution_version": "7.5.1804", 

​ 结果

​ TASK [conf] ***********************************************************************************************

skipping: [192.168.113.150]

最终yaml

[root@node1 ansibletest]# cat testtemplate.yaml
- hosts: all
  remote_user: root
  vars: #变量
  - http_port: 88 #会应用到nginx的配置文件中
 
  tasks:
  - name: install nginx
    yum: name=nginx
  - name: copy temp
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
    when: ansible_distribution_major_version == "7"
    notify: restart service
  - name: start service
    service: name=nginx state=started

  handlers:
  - name: restart service
    service: name=nginx state=restarted

循环

​ 迭代,需要重复执行任务

​ 对迭代项的引用固定变量名为item

​ 要在tasks中使用with_items 给定要迭代的元素列表

列表运用
[root@node1 ansibletest]# cat useritem.yaml 
- hosts: all
  remote_user: root

  tasks:
   - name: create file
     file: name=/root/{{ item }} state=touch
     with_items:  
      - file1
      - file2
   - name: instal pks
     yum: name={{ item }}
     with_items:
      - htop
      - sl
字典运用
[root@node1 ansibletest]# cat group.yaml 
- hosts: all
  remote_user: root

  tasks:
   - name: create group
     group: name={{ item }} 
     with_items:
      - g1
      - g2
   - name: create users
     user: name={{item.name}} group={{item.group}}  #字典
     with_items:
      - { name: 'user1', group: 'g1' }
      - { name: 'user2', group: 'g2' }
for循环
[yaml]# cat for.yaml 
- hosts: all  #主机
  remote_user: root  #用户
  vars:  #变量定义
   ports: #变量名称
    - 81  #变量列表
    - 82
    - 83

  tasks: #任务
   - name: copy conf  #任务名称
     template: src=for1.conf.j2 dest=/root/for1.conf  #任务动作
     
[循环体]# cat templates/for1.conf.j2 
{% for port in ports%}  #for循环
server{
       listen {{ port }}   #语句
}
{% endfor %} #结束语句

[root@localhost ~]# cat for1.conf
server{
listen 81
}
server{
listen 82
}
server{
listen 83
}

字典应用
[root@node1 ansibletest]# cat word-for2.yaml
- hosts: all
  remote_user: root
  vars:
   words:
    - wd: hello #键值对
    - wd: world

  tasks:
   - name: cp file
     template: src=words2.j2 dest=/root/words2.txt
     
# cat templates/words2.j2 
{% for word in words %}
hi{
	welcome {{ word.wd }}
}
{% endfor %}

循环列表字典

[root@node1 ansibletest]# cat  word-for2.yaml 
- hosts: all
  remote_user: root
  vars:
   words:
    - wd: hello   #列表+字典
      time: 2019

  tasks:
   - name: cp file
     template: src=words2.j2 dest=/root/words2.txt
     
# cat  templates/words2.j2 
{% for word in words %}
hi{
	welcome {{ word.wd }}
	welcome back {{ word.time}}
}
{% endfor %}


[root@node1 ansibletest]# cat  word-for2.yaml 
- hosts: all
  remote_user: root
  vars:
   words:
    - wd: hello   #列表+字典
      time: 2019

  tasks:
   - name: cp file
     template: src=words2.j2 dest=/root/words2.txt
     
[root@node1 ansibletest]# cat templates/words2.j2
{% for word in words %}
hi{
	welcome {{ word.wd }}
{% if word.time is defined %}  #if判断,如果存在就定义
	welcome back {{ word.time}}
{% endif %}
}
{% endfor %}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值