Ansible技巧提示
1 - 免密登录
# 通过秘钥方式连接
ssh-keygen -t rsa
ssh-copy-id -i /vipxf/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /vipxf/.ssh/id_rsa.pub [email protected]
2 - playbook输入root密码
[root@test01 ansible-test]# cat test-temp.yaml
---
- name: test-root
hosts: ta
remote_user: vipxf
gather_facts: no
become: yes
become_user: root
become_method: su
become_flags: "-"
vars:
ansible_become_pass: "{{ root_pass_input }}"
tasks:
- name: test
shell: date && sleep 3
ignore_errors: True
[root@test01 ansible-test]#
[root@test01 ansible-test]# ansible-playbook test-temp.yaml -e root_pass_input=redhat
PLAY [test-root] **************************************************************************************************************************************************************************************************************
TASK [test] *******************************************************************************************************************************************************************************************************************
changed: [172.20.8.247]
PLAY RECAP ********************************************************************************************************************************************************************************************************************
172.20.8.247 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@test01 ansible-test]#
3 - 简单的复用方式
通过关键字include可以实现简单的代码复用。
# 在playbook中,Include 指令可以跟task 混合在一起使用
- tasks:
- include: tasks/foo.yml # 引用配置文件
# 参数化的include
- tasks:
- include: aaa.yml user=anliven # 引用配置文件的同时传入变量
- include: bbb.yml pwd=P@ssw0rd
4 - task通知多个handlers任务
通过关键字listen为handlers中的一个或多个触发器任务指定主题,task就可以按主题名进行通知。
触发顺序按照handler定义的先后顺序执行。
---
- hosts: ta
remote_user: vipxf
gather_facts: no
vars:
- package: vim
tasks:
- name: test-step one
shell: echo "111"
notify: "test listen" # task通知handlers主题
- name: test-step two
shell: echo "222"
handlers:
- name: check-hostname-date
shell: hostname && date
ignore_errors: True
listen: "test listen" # 按照定义顺序被触发
- name: who-am-i
shell: whoami && date
ignore_errors: True
listen: "test listen" # 按照定义顺序被触发
标签:include,技巧,提示,ansible,Ansible,test,listen,root,name From: https://www.cnblogs.com/anliven/p/16857920.html