1.template1
hosts定义了变量
[websrvs]
172.16.62.12 http_port=8081
172.16.62.13 http_port=8082
[root@ansible ansible]# more testtempl.yml
---
- hosts: websrvs
remote_user: root
vars:
- http_port: 8088 #定义了变量
tasks:
- name: install package
yum: name=nginx
- name: copy template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart service
- name: start service
service: name=nginx state=started enabled=yes
handlers:
- name: restart service
service: name=nginx state=restarted
[root@ansible ansible]#
- 变量优先级
最高 ansible-playbook -e
playbook
主机清单
普通变量
公共变量
1.2.执行
[root@ansible ansible]# ansible-playbook testtempl.yml
PLAY [websrvs] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [172.16.62.13]
ok: [172.16.62.12]
TASK [install package] *******************************************************************************************************************************************************************
ok: [172.16.62.13]
ok: [172.16.62.12]
TASK [copy template] *********************************************************************************************************************************************************************
changed: [172.16.62.13]
changed: [172.16.62.12]
TASK [start service] *********************************************************************************************************************************************************************
ok: [172.16.62.13]
ok: [172.16.62.12]
RUNNING HANDLER [restart service] ********************************************************************************************************************************************************
changed: [172.16.62.13]
changed: [172.16.62.12]
PLAY RECAP *******************************************************************************************************************************************************************************
172.16.62.12 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.62.13 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.3 验证结果
[root@ansible ansible]# ansible websrvs -m shell -a 'ss -tnl'
172.16.62.13 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:11211 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:81 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:8088 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:8088 [::]:*
172.16.62.12 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:81 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:8088 *:*
LISTEN 0 128 *:11211 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:8088 [::]:*
[root@ansible ansible]
2.template2
查看系统版本
ansible_distribution_version
[root@ansible templates]# ansible all -m setup -a 'filter="*distribution*"'
172.16.62.14 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.8",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[WARNING]: No python interpreters found for host 172.16.62.241 (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-
python', '/usr/bin/python3', 'python'])
172.16.62.241 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "VMkernel",
"ansible_distribution_release": "6.5.0",
"ansible_distribution_version": "#1 SMP Release build-14320405 Aug 5 2019 02:37:27",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[WARNING]: No python interpreters found for host 172.16.62.240 (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-
python', '/usr/bin/python3', 'python'])
172.16.62.240 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "VMkernel",
"ansible_distribution_release": "6.5.0",
"ansible_distribution_version": "#1 SMP Release build-14320405 Aug 5 2019 02:37:27",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
172.16.62.11 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.2",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
172.16.62.13 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.8",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
172.16.62.12 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.8",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
#ansible-playbook
[root@ansible ansible]# more testtempl.yml
---
- hosts: websrvs
remote_user: root
vars:
- http_port: 8088
tasks:
- name: install package
yum: name=nginx
- name: copy template for centos7.2
template: src=nginx.conf7.2.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_version == "7.2"
notify: restart service
- name: copy template for centos7.8
template: src=nginx.conf7.8.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_version == "7.8"
notify: restart service
- name: start service
service: name=nginx state=started enabled=yes
handlers:
- name: restart service
service: name=nginx state=restarted
[root@ansible ansible]#
2.2 开始执行
[root@ansible ansible]# ansible-playbook testtempl.yml
PLAY [websrvs] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [172.16.62.11]
ok: [172.16.62.12]
TASK [install package] *******************************************************************************************************************************************************************
ok: [172.16.62.12]
changed: [172.16.62.11]
TASK [copy template for centos7.2] *******************************************************************************************************************************************************
skipping: [172.16.62.12]
changed: [172.16.62.11]
TASK [copy template for centos7.8] *******************************************************************************************************************************************************
skipping: [172.16.62.11]
ok: [172.16.62.12]
TASK [start service] *********************************************************************************************************************************************************************
ok: [172.16.62.12]
changed: [172.16.62.11]
RUNNING HANDLER [restart service] ********************************************************************************************************************************************************
changed: [172.16.62.11]
PLAY RECAP *******************************************************************************************************************************************************************************
172.16.62.11 : ok=5 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
17
2.3 验证
[root@ansible ansible]# ansible websrvs -m shell -a 'ss -tnl'
172.16.62.11 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:8088 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 32 :::21 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::8088 :::*
172.16.62.12 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:81 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:8088 *:*
LISTEN 0 128 *:11211 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:8088 [::]:*
[root@ansible ansible]#