ansible:如何更改$ HOME目录

时间:2020-03-14 20:33:36

标签: ansible

我在控制机ansible 2.9.6中运行Ubuntu 18.04 Desktop 控制没有Ubuntu 16.04目录的单个服务器/home/username/服务器。 我不打算再创造一个。

我只是想从控制机的从机中创建一个新文件夹“ / usr / local / src / fromcontrolmachine”

所以我遇到了命令

 dinesh@dinesh-VirtualBox:/etc/ansible$ ansible all -u dira
 --become -m file -a "dest=/usr/local/src/fromcontrolmachine mode=755 owner=dira group=dira state=directory" -K 

成为密码:

> 10.211.108.44 | FAILED! => {
>     "changed": false, 
>     "module_stderr": "Shared connection to 10.211.108.44 closed.\r\n", 
>     "module_stdout": "Could not chdir to home directory /home/dira: No such file or directory\r\n\r\n", 
>     "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
>     "rc": 1 }

我想通过在下面的行中添加来更改$ Home目录 /etc/ansible/ansible.cfg。它只是创建了一个名为ansible

的空文件夹
 remote_tmp = usr/local/src/ansible

如何通过指向默认$Home之外的其他位置来告诉ansible更改默认/home/dira目录?

我想清除这个烦人的错误 “ module_stdout”:“无法chdir到主目录/ home / dira271641:没有这样的文件或目录

更新:

还尝试创建剧本pb.yml并添加home_dir: /usr/local/src/ansible,如下所述。

---

- hosts: all
  become: true
  tasks:
    - set_fact:
        home_dir: /usr/local/src/ansible
      become: true
    - name: ansible create directory example
      file:
        path: /tmp/devops_directory
        state: directory

当我使用命令ansible-playbook pb.yml -K

在上面运行时

但是它给出了与上述相同的错误。

更新: 我尝试了environment: HOME:

---
- hosts: all
  become: true
  environment: 
    HOME: /usr/local/src/ansible
  tasks:
  - name: ansible create directory example
    file:
      path: /tmp/devops_directory
      state: directory

引发相同错误

Could not chdir to home directory /home/dira: No such file or directory\r\n\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}}, "msg": "The following modules failed to execute: setup\n"}

1 个答案:

答案 0 :(得分:1)

添加以下行。

become_user: dira

解决了这个问题。注意:dira是我的用户名。因此,请替换您的用户名。 因此完整的剧本脚本看起来像

---
- hosts: all
  become: true
  become_user: dira
  environment: 
    HOME: /usr/local/src/ansible
  tasks:
  - name: ansible create directory example
    file:
      path: /tmp/devops_directory
      state: directory