初识play中的connnection: local
先看下建立主机互信和分发主机公钥信息的栗子:
- name: configure ssh connection
hosts: new
gather_facts: false
connection: local
tasks:
- name: configure ssh connection
shell:
ssh-keyscan {{inventory_hostname}} >> ~/.ssh/known_hosts
sshpass -p'xxxxxxx' ssh-copy-id root@{{inventory_hostname}}
解释一下,上述play中定义了 play级别connection连接配置信息,意思就是在Ansible本地执行 N次(由筛选出的inventory主机数量决定),如果有多个tasks,那么每个task都将执行N次。
利用script模块在受控端执行Ansible上的shell脚本
- name: use script module
hosts: new
gather_facts: false
tasks:
- name: use script module
script: /tmp/hello.sh HELLOWORLD
执行结果:
[root@pg01 ~]# ansible-playbook -v hello_script.yaml
Using /etc/ansible/ansible.cfg as config file
PLAY [use some module] *********************************************************************************************************************************************************************************************
TASK [use script module] *******************************************************************************************************************************************************************************************
changed: [10.0.31.92] => {"changed": true, "rc": 0, "stderr": "Shared connection to 10.0.31.92 closed.\r\n", "stderr_lines": ["Shared connection to 10.0.31.92 closed."], "stdout": "hello world: HELLOWORLD\r\n", "stdout_lines": ["hello world: HELLOWORLD"]}
changed: [10.0.31.91] => {"changed": true, "rc": 0, "stderr": "Shared connection to 10.0.31.91 closed.\r\n", "stderr_lines": ["Shared connection to 10.0.31.91 closed."], "stdout": "hello world: HELLOWORLD\r\n", "stdout_lines": ["hello world: HELLOWORLD"]}
PLAY RECAP *********************************************************************************************************************************************************************************************************
10.0.31.91 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.31.92 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
网友评论