运行我的剧本时,我在连接到local_action时遇到问题,我的剧本用于在SQL Server中创建用户,需要运行本地操作才能生成随机密码
fatal: [w961412]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='localhost', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f4572858790>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
ssh的任务
- name: get random password
command: tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=12 count=1 status=none
register: secret
winrm的任务:
- name: Alter database credentials.
win_shell: |
sqlcmd -S {{ sql_hostname }},{{ SQL_PORT }} -E -q "alter login {{ dbuser }} with password=N'{{ secret.password }}'" -o alter.log
register: alter_result
local动作;我认为它不需要通过ssh重新连接到您的控制主机即可生成密码
所以,我希望您可以使用:- name: get a random password
connection: local
shell: tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=12 count=1 status=none
register: secret
- win_shell: |
echo "and now you are back to the normal playbook connection"
您的代码段中也存在一个错误,即尝试将command:
与包含管道的字符串一起使用-command:
不支持shell运算符,这就是shell:
存在的原因然后,您不必使用一堆shell命令以及一些神奇的
tr
字符串文字:ansible具有一个random password lookup,因此您可以:
- win_shell: | sqlcmd -q "alter login with password=N'{{ item }}'" register: alter_result with_password: /dev/null length=12