无法使用 become_user 切换到其他用户
Unable to switch to a different user using become_user
我正在编写 Ansible 剧本以在具有特定用户的多个主机上创建基于密钥的 ssh 访问。
我有以下服务器:
automation_host
硕士
从站1
从机2
从自动化主机我将触发 Ansible 到 运行 应该首先使用 user1 登录到 master 的 playbook,然后切换到 user2,使用 user2 创建 ssh 密钥并将 id_rsa.pub 复制到从节点.
清单文件内容:
[master]
172.xxx.xxx.xxx
[slaves]
172.xxx.xxx.xxx
172.xxx.xxx.xxx
[all:vars]
ansible_connection=ssh
ansible_ssh_user=user1
playbook.yml 文件:
- hosts: master
become_user: user2
become: yes
roles:
- name: passwordless-ssh
User2 在所有主机(automation_host 除外)上可用,并且也在 sudoers
中添加。
在 passwordless-ssh 角色中,我添加了下面包含的行以检查哪个用户当前正在执行任务。
- name: get the username running the deploy
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
调试消息显示用户 1(我希望它是用户 2)
可靠的版本:2.5.2
我是 Ansible 的新手。
local_action
将 run on automation_host,改为 command
- hosts: master
become_user: user2
become: yes
tasks:
- name: get the username running the deploy
command: whoami
register: username_on_the_host
- debug: var=username_on_the_host['stdout']
- name: do something
command: echo 'hello'
when: username_on_the_host['stdout'] == 'user2'
- name: do something else
command: echo 'goodby'
when: username_on_the_host['stdout'] == 'user1'
输出
TASK [debug] *********************************************
ok: [master] => {
"username_on_the_host['stdout']": "user2"
}
TASK [do something] *********************************************
changed: [master]
TASK [do something else] *********************************************
do something else
不 运行.
我正在编写 Ansible 剧本以在具有特定用户的多个主机上创建基于密钥的 ssh 访问。 我有以下服务器:
automation_host
硕士
从站1
从机2
从自动化主机我将触发 Ansible 到 运行 应该首先使用 user1 登录到 master 的 playbook,然后切换到 user2,使用 user2 创建 ssh 密钥并将 id_rsa.pub 复制到从节点.
清单文件内容:
[master]
172.xxx.xxx.xxx
[slaves]
172.xxx.xxx.xxx
172.xxx.xxx.xxx
[all:vars]
ansible_connection=ssh
ansible_ssh_user=user1
playbook.yml 文件:
- hosts: master
become_user: user2
become: yes
roles:
- name: passwordless-ssh
User2 在所有主机(automation_host 除外)上可用,并且也在 sudoers
中添加。
在 passwordless-ssh 角色中,我添加了下面包含的行以检查哪个用户当前正在执行任务。
- name: get the username running the deploy
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
调试消息显示用户 1(我希望它是用户 2) 可靠的版本:2.5.2
我是 Ansible 的新手。
local_action
将 run on automation_host,改为 command
- hosts: master
become_user: user2
become: yes
tasks:
- name: get the username running the deploy
command: whoami
register: username_on_the_host
- debug: var=username_on_the_host['stdout']
- name: do something
command: echo 'hello'
when: username_on_the_host['stdout'] == 'user2'
- name: do something else
command: echo 'goodby'
when: username_on_the_host['stdout'] == 'user1'
输出
TASK [debug] *********************************************
ok: [master] => {
"username_on_the_host['stdout']": "user2"
}
TASK [do something] *********************************************
changed: [master]
TASK [do something else] *********************************************
do something else
不 运行.