使用以下命令执行剧本时,我失败并显示
Could not find or access '/opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option
ansible-playbook playbooks/install_aap_platform.yml -f 10 -vvv --private-key $ID_RSA -u ec2-user -i installer_node
这是 ansible 剧本:
- name: install ansible
hosts: installnode
become: yes
become_user: ansible
gather_facts: yes
tasks:
- name: populate /etc/hosts file
ansible.builtin.shell: |
cat {{ lookup('env', 'ANSIBLE_INSTALLER_LOCATION') }}/inventory | grep ".med.ds.osd.mil*" | awk '{print $1}' > test.txt
for i in $(cat test.txt); do
echo $i
if grep -q "$(echo "$i")" /etc/hosts ; then
echo "hostname $host_name already in hosts file"
else
echo $(echo $i | cut -d'.' -f1 | sed 's/ip-//' | sed 's/-/./g')" $i" >> /etc/hosts
fi
done
cat /etc/hosts
become: yes
become_user: root
- name: Run installer script
become: yes
become_user: ansible
ansible.builtin.script:
cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh
environment:
ANSIBLE_BECOME_METHOD: 'sudo'
ANSIBLE_BECOME: True
ANSIBLE_HOST_KEY_CHECKING: False
register: out
ignore_errors: yes
- name: print output
debug:
msg: "{{ out.stdout }}"
populate /etc/hosts file
工作没有任何问题,这是任务 Run installer script
失败了。
任务失败,因为它无法访问文件 /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh,但如果我 ssh 到主机并粘贴
/opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh
并按输入,立即生效。
脚本似乎不是问题,文件夹 /opt 由 root 所有,但文件夹 ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/ 由用户 ansible 所有。 我知道 Bebe_user 可以工作,因为我在之前的任务中使用它来将文件上传到主机系统,没有任何问题。
script
模块将本地脚本复制到远程机器。当你有:
ansible.builtin.script:
cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh
这意味着它将在本地(控制)主机上查找
/opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh
,而不是在作为播放目标的主机上。如果脚本已存在于远程位置,您应该使用 command
模块:
ansible.builtin.command:
cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh
与您的问题无关,但在
populate /etc/hosts file
中,您不需要 cat
或 grep
;你可以写:
awk '/.med.ds.osd.mil/ {print $1}' {{ lookup('env', 'ANSIBLE_INSTALLER_LOCATION') }}/inventory > test.txt
...老实说,您可以用 Ansible 循环和
lineinfile
模块替换整个脚本。