2、playbook执行的命令
写一个playbook
[root@foundation50 .ansible]# vim test.yml
---
- name: test yml
hosts: 172.25.254.100
tasks:
- name: action
shell:
hostname
[root@foundation50 .ansible]# ansible-playbook -C test.yml 对 test.yml 进行语法检测, -C表示执行检测
[root@foundation50 .ansible]# ansible-playbook --syntax-check test.yml --syntax-check语法检测
[root@foundation50 .ansible]# ansible-playbook --list-hosts test.yml 列出运行主机
playbook: test.yml
play #1 (172.25.254.100): test yml TAGS: []
pattern: ['172.25.254.100']
hosts (1):
172.25.254.100
[root@foundation50 .ansible]# ansible-playbook --list-tags test.yml 列出tags
playbook: test.yml
play #1 (172.25.254.100): test yml TAGS: []
TASK TAGS: []
[root@foundation50 .ansible]# ansible-playbook --list-tasks test.yml 列出动作
playbook: test.yml
play #1 (172.25.254.100): test yml TAGS: []
tasks:
action TAGS: []
[root@foundation50 .ansible]# ansible-playbook --limit 172.25.254.200 test.yml -C 指定执行主机,但是hosts列表里面没有200这个主机
PLAY [test yml] *******************************************************************************************
skipping: no hosts matched
PLAY RECAP *******************************************************
[root@foundation50 .ansible]# vim test.yml
---
- name: test yml
hosts: westos 将hosts列表172,25.254.100改成westos
tasks:
- name: action
shell:
hostname
[root@foundation50 .ansible]#ansible-playbook --limit 172.25.254.200 test.yml -C 指定200这台主机执行,westos列表清单里有200这台主机
PLAY [test yml] *******************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [172.25.254.200]
TASK [action] *********************************************************************************************
skipping: [172.25.254.200]
[root@foundation50 .ansible]# ansible-playbook test.yml -v -v 表示显示过程 -vv跟详细
在书写playbook时建议:
[root@foundation50 .ansible]# vim ~/.vimrc
1 set nu ts=2 ai et ts=2 表示tab建表示两个空格 ai表示自动缩进 et表示把tab建转换称空格
3 playbook简单示例及vim设定
[root@foundation50 .ansible]# vim westos.yml
---
- name: test play
hosts: 172.25.254.100
tasks:
- debug:
msg: hello world
[root@foundation50 .ansible]# ansible-playbook westos.yml
PLAY [test play] 此处就是name名称,可以不写******************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [172.25.254.100]
TASK [debug] **********************************************************************************************
[root@foundation50 .ansible]# cat westos.yml
---
- hosts: 172.25.254.100 把name名称可以直接去掉
tasks:
- debug:
msg: hello world
[root@foundation50 .ansible]# ansible-playbook westos.yml
ok: [172.25.254.100] => {
"msg": "hello world"
1 ---
2 - hosts: 172.25.254.100
3 tasks:
4 - debug:
5 msg: hello world
注意:hosts 和 tasks 都必须写
示例:用playbook部属apache
[root@foundation50 .ansible]# vim apache.yml
---
- name: Apache server
hosts: 172.25.254.100
tasks:
- name: install apache
dnf:
name: httpd
state: latest
disable_gpg_check: yes 不检测
- name: start apache
service:
name: httpd
state: started
enabled: yes
- name: firewalld setup
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
zone: public
- name: create index
lineinfile:
path: /var/www/html/index.html
line: "hello westos"
create: yes
[root@foundation50 .ansible]# ansible-playbook apache.yml -C 执行检测
[root@foundation50 .ansible]# ansible-playbook apache.yml 运行
当不会写的时可以 查看模块帮助[root@foundation50 westos]# ansible-doc firewalld | less
vim设定
[root@foundation50 .ansible]# vim ~/.vimrc
1 autocmd FileType yaml setlocal ts=2 ai et sw=2 setlocal表示只对当前yaml文件生效
2 set nu 针对所有