在local_action针对一组主机运行 playbook 时,是否可以使任务仅运行一次?
这是问题所在:
hosts:
- macbooks
- localhost
tasks:
#...<some_remote_tasks>...#
- local_action: command
ssh-keygen -o -a 100 -t ed25519 -f {{ ssh_key }} -q -N ''
become: yes
Run Code Online (Sandbox Code Playgroud)
结果:
fatal: [laptop -> localhost]: FAILED! => {"changed": true, "cmd": ["ssh-keygen", "-o", "-a", "100", "-t", "ed25519", "-f", "/etc/ssh/id_ed25519-HostCA", "-q", "-N", "", "-C", "SSH Host Certificate Authority for cypherpunk.synology.me"], "delta": "0:00:00.014818", "end": "2018-06-01 17:02:41.599111", "msg": "non-zero return code", "rc": 1, "start": "2018-06-01 17:02:41.584293", "stderr": "", "stderr_lines": [], "stdout": "/etc/ssh/id_ed25519-HostCA already exists.\nOverwrite (y/n)? ", "stdout_lines": ["/etc/ssh/id_ed25519-HostCA already exists.", "Overwrite (y/n)? "]}
changed: [localhost -> localhost]
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为剧本中的任何任务都必须为每个托管主机运行。
但是因为它是一个本地操作,所以它会按预期在创建密钥文件时第一次运行。第二次该文件已经存在并且 ansible 收到错误:"/etc/ssh/id_ed25519-HostCA already exists. Overwrite (y/n)?"使用return code 1. 所以实际上它必须只运行一次(至少在这种情况下)。
我可以做这样的事情:
- local_action: shell >
[[ ! -f {{ ssh_key }} ]] && \
ssh-keygen -o -a 100 -t ed25519 -f {{ ssh_key }} -q -N ''; \
exit 0
become: yes
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有 ansible-recommended 解决方案?大家会怎么解决这个问题?
小智 5
也许你应该检查:run_once & delegate_to
- command: /opt/application/upgrade_db.py
run_once: true
delegate_to: web01.example.org
Run Code Online (Sandbox Code Playgroud)
文档:https : //docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html
亲切的问候,
C
| 归档时间: |
|
| 查看次数: |
8672 次 |
| 最近记录: |