之前我们最常用的就是命令模式,也简称为“临时命令模式”。如下:
[root@chuangxindasai-prometheus ansible]# ansible ceph -m ping
172.18.0.132 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.18.0.133 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.18.0.131 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@chuangxindasai-prometheus ansible]# ansible ceph -m shell -a "hostname"
172.18.0.131 | CHANGED | rc=0 >>
ceph-admin
172.18.0.133 | CHANGED | rc=0 >>
ceph-node2
172.18.0.132 | CHANGED | rc=0 >>
ceph-node1
这只能做一下简单的操作,如果遇到复杂的操作就不试用了。下面记录一下简单的剧本操作
# 剧本以yaml 格式
# 该剧本里面包含了两个剧本
[root@chuangxindasai-prometheus ansible]# cat test.yml
---
# 第一个剧本(hosts 定义主机组)
- hosts: ceph
tasks:
- name: install httpd
# 定义yum 模块
yum:
name: httpd
state: latest
# 第二个剧本
- hosts: ceph
tasks:
- name: restart firewalld
service:
name: firewalld
state: restarted
# 一个剧本中多个task,{{ ansible_ssh_pass }} 还调用了vars 中的参数
---
- hosts: ceph
tasks:
- name: install httpd {{ ansible_ssh_pass }}
yum:
name: httpd
state: latest
- name: restart firewalld
service:
name: firewalld
state: restarted
- name: command
command: echo `hostname`
# 上面调用的参数在下面这里配置的
[root@chuangxindasai-prometheus ansible]# cat /etc/ansible/group_vars/ceph.yml
---
ansible_ssh_port: 22
ansible_ssh_user: root
ansible_ssh_pass: ccssoft
运行playbook
[root@chuangxindasai-prometheus ansible]# ansible-playbook /opt/ansible/test2.yml
PLAY [ceph] ******************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [172.18.0.133]
ok: [172.18.0.132]
ok: [172.18.0.131]
TASK [install httpd ccssoft] *************************************************************************************************************************************************************
ok: [172.18.0.131]
ok: [172.18.0.133]
ok: [172.18.0.132]
TASK [restart firewalld] *****************************************************************************************************************************************************************
changed: [172.18.0.132]
changed: [172.18.0.133]
changed: [172.18.0.131]
TASK [command] ***************************************************************************************************************************************************************************
changed: [172.18.0.132]
changed: [172.18.0.131]
changed: [172.18.0.133]
PLAY RECAP *******************************************************************************************************************************************************************************
172.18.0.131 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.0.132 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.18.0.133 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
来源:oschina
链接:https://my.oschina.net/wangzilong/blog/4454421