我的ansible服务器以帐户access_user访问目标主机。
[defaults]
remote_user = access_user
private_key_file = /home/roger/access_user/id_rsa
在我的剧本中,操作(实际上按角色组织)需要以config_user身份运行,因此我使用了begin_user关键字。 同时,一些任务需要在ansible服务器上本地运行,所以我使用了委托委托。
- name: test
hosts: pltvip
become: true
become_user: config_user
become_method: sudo
gather_facts: no
roles:
- role: 'do_configure'
role do_configure:
- name: create local directory
shell: mkdir /storage/savelog
delegate_to: localhost
- name: do something
shell: myscript.sh
剧本因以下错误而失败:
任务[do_configure:创建本地目录] ******************************************* **********************************任务路径:/home/roger/ansible/configure/do_configure/tasks/main.yml: 2 为用户建立本地连接:roger EXEC / bin / sh -c'回声〜罗杰&&睡眠0' EXEC / bin / sh -c'(umask 77 && mkdir -p“
echo /var/tmp/ansible-tmp-1570882787.55-210995749037992
” && echo ansible-tmp-1570882787.55-210995749037992 =“echo /var/tmp/ansible-tmp-1570882787.55-210995749037992
”)&& sleep 0' 使用模块文件/usr/lib/python2.7/dist-packages/ansible/modules/commands/command.py 将/home/roger/.ansible/tmp/ansible-local-129262NOr73/tmpSjAO8_复制到/ var / tmp / ansible-tmp-1570882787。 55-210995749037997992 / AnsiballZ_command.py EXEC / bin / sh -c'setfacl -m u:dsuser:r-x /var/tmp/ansible-tmp-1570882787.55-210995749037992/ / var / tmp / ansible-tmp-1570882787.55-210995749037997992 / AnsiballZ_command.py && sleep 0' EXEC / bin / sh -c'chmod u + x /var/tmp/ansible-tmp-1570882787.55-210995749037992/2/var/tmp/ansible-tmp-1570882787.55-210995749037992/AnsiballZ_command.py && sleep 0'EXEC / bin / sh -c'chown config_user /var/tmp/ansible-tmp-1570882787.55-210995749037992/ / var / tmp / ansible-tmp -1570882787.55-210995749037997992 / AnsiballZ_command.py && sleep 0' 致命的:[192.168.197.53]:失败! => { “ msg”:“无法对临时文件设置权限当成为非特权时,Ansible需要创建 用户(rc:1,err:chown:无效用户:“ config_user” \ n})。有关解决此问题的信息,请参见https://docs.ans ible.com/ansible/become.html#becoming-an-unprivileged-user“ }
我删除了begin_user,然后此错误消失了。唯一的区别是这次没有“小丑”动作。
答案 0 :(得分:0)
不要把付出放在第一位,而要把它提供给您需要的特定任务。
- name: test
hosts: pltvip
become_user: config_user
become_method: sudo
gather_facts: no
roles:
- role: 'do_configure'
role do_configure:
- name: create local directory
shell: mkdir /storage/savelog
delegate_to: localhost
- name: do something
shell: myscript.sh
become: true