#hosts 设置
[root@ansible ansible]# more /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
[websrvs]
172.16.62.12 http_port=81 #单台主机定义变量
172.16.62.13 http_port=82
[websrvs:vars] #对组定义变量
nodename=web
domainname=aliyun.com
#hostname.yml
[root@ansible ansible]# ansible-playbook hostname.yml
PLAY [websrvs] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [172.16.62.13]
ok: [172.16.62.12]
TASK [set hostname] **********************************************************************************************************************************************************************
changed: [172.16.62.13]
changed: [172.16.62.12]
PLAY RECAP *******************************************************************************************************************************************************************************
172.16.62.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.62.13 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#查看结果
[root@ansible ansible]# ansible websrvs -m shell -a 'hostname'
172.16.62.13 | CHANGED | rc=0 >>
web82.aliyun.com
172.16.62.12 | CHANGED | rc=0 >>
web81.aliyun.com
7.变量4
#定义变量
[root@ansible ansible]# cat vars.yml
var1: httpd
var2: vsftpd
[root@ansible ansible]#
# 调用变量var.yml
[root@ansible ansible]# more testvas.yml
---
- hosts: websrvs
remote_user: root
vars_files:
- vars.yml
tasks:
- name: install package
yum: name={{var1 }}
- name: create file
file: name=/tmp/{{ var2 }}.log state=touch
[root@ansible ansible]#
# 执行playbook
[root@ansible ansible]# ansible-playbook testvas.yml
PLAY [websrvs] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [172.16.62.12]
ok: [172.16.62.13]
TASK [install package] *******************************************************************************************************************************************************************
ok: [172.16.62.12]
ok: [172.16.62.13]
TASK [create file] ***********************************************************************************************************************************************************************
changed: [172.16.62.12]
changed: [172.16.62.13]
PLAY RECAP *******************************************************************************************************************************************************************************
172.16.62.12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.62.13 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
## 验证
[root@ansible ansible]# ansible websrvs -m shell -a 'ls /tmp/vsftpd.log -l'
172.16.62.12 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 0 Nov 17 07:16 /tmp/vsftpd.log
172.16.62.13 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 0 Nov 17 07:16 /tmp/vsftpd.log
[root@ansible ansible]# ansible websrvs -m shell -a 'rpm -q httpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command because yum, dnf or zypper is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
172.16.62.13 | CHANGED | rc=0 >>
httpd-2.4.6-93.el7.centos.x86_64
172.16.62.12 | CHANGED | rc=0 >>
httpd-2.4.6-93.el7.centos.x86_64