Ansible 同步模块不会用准确的用户名替换用户变量

Ansible 同步模块不会用准确的用户名替换用户变量

我尝试了很多方法来解决以下问题,但我不够幸运。我有以下库存;

#cat inventory/common/group_vars/all/inventory_pwd.yml

Remote1_user:“devops”

#cat inventory/nonprod/fdc/group_vars/all/inventory.txt

[远程] xxxx ansible_ssh_user="{{remote1_user }}" ansible_port=22 ansible_password="test@12"

我正在运行下面的剧本,将文件从控制机复制到远程计算机;

PB.yml

---
- name: "Copy test"
  hosts: remote
  become: true
  tasks:

    - name: Copying content to the remote vm
      synchronize:
        src: /ansible/playbooks/Hello
        dest: /home/devops/
      delegate_to: localhost

在控制器机器中,我以root用户身份登录,连接远程服务器的用户是devops。

但是,执行剧本后,我收到以下错误;

ansible-playbook -vvv -i inventory/common -i inventory/nonprod/fdc/group_vars/all/inventory.txt pb.yml

TASK [Copying content to the remote vm] *********************************************************************************************task path: /ansible/playbooks/pb.yml:9
fatal: [x.x.x.x -> localhost]: FAILED! => {
    "changed": false,
    "cmd": "sshpass -d3 /usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --rsync-path=sudo rsync --out-format=<<CHANGED>>%i %n%L /ansible/playbooks/Hello {{remote1_user}}@x.x.x.x:/home/devops/",
    "invocation": {
        "module_args": {
            "_local_rsync_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "_local_rsync_path": "rsync",
            "_substitute_controller": false,
            "archive": true,
            "checksum": false,
            "compress": true,
            "copy_links": false,
            "delete": false,
            "dest": "{{remote1_user}}@x.x.x.x:/home/devops/",
            "dest_port": null,
            "dirs": false,
            "existing_only": false,
            "group": null,
            "link_dest": null,
            "links": null,
            "mode": "push",
            "owner": null,
            "partial": false,
            "perms": null,
            "private_key": null,
            "recursive": null,
            "rsync_opts": [],
            "rsync_path": "sudo rsync",
            "rsync_timeout": 0,
            "set_remote_user": true,
            "src": "/ansible/playbooks/Hello",
            "ssh_args": null,
            "times": null,
            "verify_host": false
        }
    },
    "msg": "remote username contains invalid characters\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(235) [sender=3.1.3]\n",
    "rc": 255
}

这里的 '"dest": "{{ remote1_user }}@xxxx:/home/devops/"' 应该像 '[电子邮件受保护]:/home/devops/'.但是,它不会将“remote1_user”替换为“devops”。

有人可以帮我解决这个问题吗?

答案1

不确定这是否是问题所在,但我总是在大括号前后用空格对变量进行编码 - {{remote1_user }}@xxxx:/home/devops/

答案2

名称可能有问题 - 不确定 ansible 是否找到变量文件。如果 inventory/nonprod/fdc/group_vars/all/inventory.txt 是您的主机文件,请尝试将其更改为 inventory/nonprod/fdc/inventory.txt,然后将组 var 文件更改为 inventory/nonprod/fdc/group_vars/all/inventory_pwd .yml

相关内容