#编写playbook 变量名为rpm,格式{{rpm}}
vim app.yml
---
- hosts: web
tasks:
- name: install app
yum: name="{{rpm}}"
- name: start app
service:
name: "{{rpm}}"
state: started
enabled: yes
给rpm变量定义为vsftpd
[root@tdm1 playbook]# ansible-playbook app.yml -e 'rpm=vsftpd'
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
TASK [start app] *******************************************************************************************************changed: [47.93.98.117]
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@tdm1 playbook]# ansible web -m shell -a 'ps -ef | grep vsftpd'
47.93.98.117 | CHANGED | rc=0 >>
root 15665 1 0 16:59 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 15880 15879 0 16:59 pts/1 00:00:00 /bin/sh -c ps -ef | grep vsftpd
root 15883 15880 0 16:59 pts/1 00:00:00 grep vsftpd
#编写playbook,在剧本中定义变量
vim app.yml
---
- hosts: web
vars:
- rpm1: httpd
- rpm2: vsftpd
tasks:
- name: install app
yum: name="{{rpm1}}"
- name: install app
yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@tdm1 playbook]# ansible web -m shell -a 'rpm -q httpd vsftpd'
[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.
47.93.98.117 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
vsftpd-3.0.2-29.el7_9.x86_64
vim /etc/ansible/hosts
[web]
47.93.98.117 rpm1=httpd rpm2=vsftpd
vim app.yml
---
- hosts: web
tasks:
- name: install app
yum: name="{{rpm1}}"
- name: install app
yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vim /etc/ansible/hosts
[web]
47.93.98.117
[web:vars]
rpm1=httpd
rpm2=vsftpd
vim app.yml
---
- hosts: web
tasks:
- name: install app
yum: name="{{rpm1}}"
- name: install app
yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vim var.yml #变量放在该文件中
rpm1: httpd
rpm2: vsftpd
vim app.yml
---
- hosts: web
vars_files:
- var.yml #变量文件,路径写清楚,我存放在了当前路径
tasks:
- name: install app
yum: name="{{rpm1}}"
- name: install app
yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@tdm1 playbook]# ansible web -m shell -a 'rpm -q httpd vsftpd'
[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.
47.93.98.117 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
vsftpd-3.0.2-29.el7_9.x86_64
mkdir group_vars
cd group_vars
vim web #创捷和hosts文件下和组一样的文件
rpm1: htpd
rpm2: vsftpd
#创建与组同名的文件
[root@tdm1 playbook]# cat /etc/ansible/hosts
[web]
47.93.98.117
vim app.yml
[root@tdm1 playbook]# cat app.yml
---
- hosts: web
tasks:
- name: install app
yum: name="{{rpm1}}"
- name: install app
yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
TASK [install app] *****************************************************************************************************changed: [47.93.98.117]
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@tdm1 playbook]# ansible web -m shell -a 'rpm -q httpd vsftpd'
[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.
47.93.98.117 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
vsftpd-3.0.2-29.el7_9.x86_64
注册变量就是将操作结果,包括标准输出和标准错误输出,保存到变量中,然后再根据这个变量的内容来决定下一步的操作,在这个过程中用来保存操作结果的变量叫注册变量
vim test.yml
---
- hosts: web
tasks:
- name: test register variables
shell: uptime
register: results
- name: print the register result
debug: msg="{{ results }}"
[root@tdm1 playbook]# ansible-playbook test.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [test register variables] *****************************************************************************************changed: [47.93.98.117]
TASK [print the register result] ***************************************************************************************ok: [47.93.98.117] => {
"msg": {
"changed": true,
"cmd": "uptime",
"delta": "0:00:00.039581",
"end": "2023-08-31 19:54:38.670898",
"failed": false,
"rc": 0,
"start": "2023-08-31 19:54:38.631317",
"stderr": "",
"stderr_lines": [],
"stdout": " 19:54:38 up 8 days, 11:29, 2 users, load average: 0.00, 0.02, 0.05",
"stdout_lines": [
" 19:54:38 up 8 days, 11:29, 2 users, load average: 0.00, 0.02, 0.05"
]
}
}
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
changed:任务是否对远程主机造成的变更。
delta:任务运行所用的时间。
stdout:正常的输出信息。
stderr:错误信息。
rc:返回值
- hosts: web
tasks:
- name: test register variables
shell: uptime
register: results
- name: print the register result
debug: msg="{{ results.stdout }}"
[root@tdm1 playbook]# ansible-playbook test.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]
TASK [test register variables] *****************************************************************************************changed: [47.93.98.117]
TASK [print the register result] ***************************************************************************************ok: [47.93.98.117] => {
"msg": " 19:55:57 up 8 days, 11:30, 2 users, load average: 0.00, 0.02, 0.05"
}
PLAY RECAP *************************************************************************************************************47.93.98.117 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0