[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 %}