为单个任务提供根用户ansible

为单个任务提供根用户ansible,ansible,ansible-playbook,Ansible,Ansible Playbook,我有主机文件 [controller] 1.1.1.1 2.2.2.2 3.3.3.3 我把我的主人设定为1.1.1.1,在组变量中 wsrep_ip=1.1.1.1 现在,我需要将一个文件从1.1.1.1复制到我为其编写任务的其他文件,如下所示 - hosts: all sudo: yes tasks: - name: Sync all configuration files across db nodes copy: src=/etc/mysql/debian

我有主机文件

[controller]
1.1.1.1
2.2.2.2
3.3.3.3
我把我的主人设定为1.1.1.1,在组变量中

wsrep_ip=1.1.1.1
现在,我需要将一个文件从1.1.1.1复制到我为其编写任务的其他文件,如下所示

- hosts: all
  sudo: yes
  tasks:
    - name: Sync all configuration files across db nodes
      copy: src=/etc/mysql/debian.cnf dest=/etc/mysql/debian.cnf
      when: "'{{ inventory_hostname }}' != '{{ wsrep_ip }}'"
      with_items: groups['controller']
如何仅针对此任务切换到根用户?有没有办法做到这一点。因为,其他任务都是用sudo运行的:是的,只有这是一个权限问题

fatal: [2.2.2.2] => error while accessing the file /etc/mysql/debian.cnf, error was: [Errno 13] Permission denied: u'/etc/mysql/debian.cnf'
fatal: [3.3.3.3] => error while accessing the file /etc/mysql/debian.cnf, error was: [Errno 13] Permission denied: u'/etc/mysql/debian.cnf'

要使单个任务以sudo方式运行,请将
sudo:yes
放在任务定义中,而不是放在播放中。即

- hosts: all
  tasks:
    - name: Sync all configuration files across db nodes
      copy: src=/etc/mysql/debian.cnf dest=/etc/mysql/debian.cnf
      when: "'{{ inventory_hostname }}' != '{{ wsrep_ip }}'"
      with_items: groups['controller']
      sudo: yes
不过,有几个注意事项

  • been
    是Ansible中
    sudo
    的新版本,因此您可以使用
    been:yes
    而不是
    sudo

  • 您可能可以使用
    delegate\u to
    关键字简化此任务定义:)

  • 编辑

    我只是正确地重新阅读了你的任务,我想我可以看出你逻辑上的错误

    copy
    模块用于将文件从本地(运行ansible的机器)框复制到任务当前运行的框中。当您在复制任务上说
    sudo:yes
    时,意味着尝试将文件保存为目标框上的root,但不会将Ansible框中的文件读取为root

    您需要使用
    delegate\u to
    使其从
    wsrep\u ip
    框复制文件

    例如(未经测试,抱歉)


    这意味着将
    copy
    操作委托给
    wsrep_ip
    框,以便
    src
    参数意味着
    wsrep_ip
    上的源文件,而不是Ansible控制机器。

    要使单个任务作为sudo运行,请将
    sudo:yes
    放在任务定义中,而不是放在播放中。即

    - hosts: all
      tasks:
        - name: Sync all configuration files across db nodes
          copy: src=/etc/mysql/debian.cnf dest=/etc/mysql/debian.cnf
          when: "'{{ inventory_hostname }}' != '{{ wsrep_ip }}'"
          with_items: groups['controller']
          sudo: yes
    
    不过,有几个注意事项

  • been
    是Ansible中
    sudo
    的新版本,因此您可以使用
    been:yes
    而不是
    sudo

  • 您可能可以使用
    delegate\u to
    关键字简化此任务定义:)

  • 编辑

    我只是正确地重新阅读了你的任务,我想我可以看出你逻辑上的错误

    copy
    模块用于将文件从本地(运行ansible的机器)框复制到任务当前运行的框中。当您在复制任务上说
    sudo:yes
    时,意味着尝试将文件保存为目标框上的root,但不会将Ansible框中的文件读取为root

    您需要使用
    delegate\u to
    使其从
    wsrep\u ip
    框复制文件

    例如(未经测试,抱歉)


    这意味着将
    copy
    操作委托给
    wsrep_ip
    框,以便
    src
    参数表示
    wsrep_ip
    上的源文件,而不是Ansible控制计算机。

    但是/etc/mysql/debian.cnf由root拥有。当我运行“ansible playbook test.yml-i hosts-u ubuntu”时,我得到了权限错误,我需要将文件复制为root。只有到那时它才会允许。对于其他用户,文件甚至不可见,但/etc/mysql/debian.cnf归root所有。当我运行“ansible playbook test.yml-i hosts-u ubuntu”时,我得到了权限错误,我需要将文件复制为root。只有到那时它才会允许。对于其他用户,该文件甚至不可见