Ansible delegate_to如何设置用于连接目标的用户?

时间:2016-07-29 13:33:36

标签: ansible ansible-2.x

我有一个Ansible(2.1.1。)库存:

build_machine ansible_host=localhost ansible_connection=local
staging_machine ansible_host=my.staging.host ansible_user=stager

我在没有ControlMaster的情况下使用SSH。

我有一个具有同步命令的剧本:

- name: Copy build to staging
  hosts: staging_machine
  tasks:
    - synchronize: src=... dest=...
      delegate_to: staging_machine
      remote_user: stager

该命令提示输入错误用户的密码:

local-mac-user@my-staging-host's password:

因此,它不使用广告资源中定义的ansible_user或任务中定义的remote_user连接到目标(播放中指定的主机),而是使用我们连接到delegate-to框的用户as,连接目标主机。

我做错了什么?我该如何解决这个问题?

编辑:它在2.0.2中工作,在2.1.x中不起作用

3 个答案:

答案 0 :(得分:0)

尝试在您的YAML文件上设置成为:yes和yes_user:stager ...这应该修复它...

https://docs.ansible.com/ansible/2.5/user_guide/become.html

答案 1 :(得分:0)

remote_user设置用于剧本级别,以用户身份设置特定的剧本。

示例:

---
- hosts: webservers
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

如果只有某些任务需要以其他用户身份运行,则可以使用becomebecome_user设置。

- name: Run command
  command: whoami
  become: yes
  become_user: some_user

最后,如果您有一组任务以用户身份在游戏中运行,则可以将它们与block分组

示例:

- block:
    - name: checkout repo
      git:
        repo: https://github.com/some/repo.git
        version: master
        dest: "{{ dst }}"
    - name: change perms
      file:
      dest: "{{ dst }}"
      state: directory
      mode: 0755
      owner: some_user
  become: yes
  become_user: some user

参考: -How to switch a user per task or set of tasks? -https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html

答案 2 :(得分:0)

最适合我的一个,但请注意,它适用于Windows和Linux,不需要become_method: runas并且基本上没有它

- name: restart IIS services
  win_service:
    name: '{{ item }}'
    state: restarted
    start_mode: auto
    force_dependent_services: true
  loop:
    - 'SMTPSVC'
    - 'IISADMIN'
  become: yes
  become_method: runas
  become_user: '{{ webserver_user }}'
  vars:
    ansible_become_password: '{{ webserver_password }}'
  delegate_facts: true
  delegate_to: '{{ groups["webserver"][0] }}'
  when: dev_env