Should be simple... Seems like command's chdir parameter doesn't work...

56 views
Skip to first unread message

Mike Eggleston

unread,
Jul 23, 2021, 10:07:26 AM7/23/21
to ansible...@googlegroups.com
Seems like the module command: parameter isn’t working. Anyone have the same error?

playbook:
---
- hosts: linux
gather_facts: yes
become: yes

vars:
file: "CarbonBlackLinuxInstaller-v6.3.4.10012-Default-Group-210309.tar.gz"
installdir: "/tmp/CarbonBlack-install"
script: "CarbonBlackClientSetup-linux-v6.3.4.10012.sh"
description: "install and start the Carbon Black Reponse agent"

tasks:
- set_fact:
file="CarbonBlackLinuxDevelopment-210721.tar.gz"
script="b9daemon.sh"
when: (ansible_host is match("hqd-") or ansible_host is match("unix-dev-") or ansible_host is match(".dev") or ansible_all_ipv4_addresses[0] is match ("192.168.60.")) == true

- name: "create {{installdir}}"
file:
path="{{installdir}}"
mode="0755"
state="directory"

- name: "copy {{file}} to the {{ansible_host}}"
copy:
src="../templates/{{file}}"
dest="{{installdir}}"
owner="root"
group="root"
mode="0644"
backup="yes"

- name: "extract {{file}}"
command:
cmd="tar -xzvf {{file}}"
chdir="{{installdir}}"
register: x

- debug:
msg="installdir='{{installdir}}' file='{{file}}' shell='{{x}}"

- fail: msg="doing fail for testing"

- name: "run {{script}}"
command:
cmd="sh {{script}}"
chdir="{{installdir}}"

- name: "delete {{installdir}}"
file:
path: "{{installdir}}"
state: absent

output:
ansible-playbook --limit hqd-dbora-n2 carbonblackresponse.yml

PLAY [linux] ******************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************
ok: [hqd-dbora-n2]

TASK [set_fact] ***************************************************************************************************************************************
ok: [hqd-dbora-n2]

TASK [create /tmp/CarbonBlack-install] ****************************************************************************************************************
ok: [hqd-dbora-n2]

TASK [copy {{file}} to the {{ansible_host}}] **********************************************************************************************************
ok: [hqd-dbora-n2]

TASK [extract CarbonBlackLinuxDevelopment-210721.tar.gz] **********************************************************************************************
fatal: [hqd-dbora-n2]: FAILED! => {"changed": false, "cmd": "'cmd=tar -xzvf CarbonBlackLinuxDevelopment-210721.tar.gz'", "msg": "[Errno 2] No such file or directory", "rc": 2}

PLAY RECAP ********************************************************************************************************************************************
hqd-dbora-n2 : ok=4 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Stefan Hornburg (Racke)

unread,
Jul 23, 2021, 10:24:26 AM7/23/21
to ansible...@googlegroups.com
On 23/07/2021 16:07, Mike Eggleston wrote:
> Seems like the module command: parameter isn’t working. Anyone have the same error?

Hello Mike,

the tar binary is probably missing in the $PATH Ansible is using. The $PATH is most likely different compared to $PATH
the interactive shell is using on the host.

Also consider to use the unarchive module instead.

Regards
Racke
--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

Mike Eggleston

unread,
Jul 23, 2021, 10:53:33 AM7/23/21
to ansible...@googlegroups.com
I looked for a “tar” module; I had forgotten about “unarchive”. I have changed my playbook and the relevant changed sections are below. I still get an error:

The changed sections of the playbook:
    - name: "copy {{file}} to the {{ansible_host}}"
      copy:
        src="../templates/{{file}}"
        dest="{{installdir}}/{{file}}"
        owner="root"
        group="root"
        mode="0644"
        backup="yes"
 
    - name: "extract {{file}}"
      unarchive:
        src="{{installdir}}/{{file}}"
        dest="{{installdir}}"
      register: x
 
    - debug:
        msg="installdir='{{installdir}}' file='{{file}}' command='{{y}}' unarchive='{{x}}"
 
    - fail: msg="doing fail for testing"

Output from: ansible-playbook —limit had-dbora-s1 -vvv carbonblackresponse.yml
2021-07-23 10:38:59,326 p=3699 u=meggleston n=ansible | TASK [extract CarbonBlackLinuxDevelopment-210721.tar.gz] **********************************************************************************************
2021-07-23 10:38:59,346 p=3751 u=meggleston n=ansible | <hqd-dbora-n2> ESTABLISH SSH CONNECTION FOR USER: None
2021-07-23 10:38:59,347 p=3751 u=meggleston n=ansible | <hqd-dbora-n2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/meggleston/.ansible/cp/7058214ea1 hqd-dbora-n2 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /tmp `"&& mkdir "` echo /tmp/ansible-tmp-1627051139.35-3751-266795384375758 `" && echo ansible-tmp-1627051139.35-3751-266795384375758="` echo /tmp/ansible-tmp-1627051139.35-3751-266795384375758 `" ) && sleep 0'"'"''
2021-07-23 10:38:59,399 p=3751 u=meggleston n=ansible | <hqd-dbora-n2> (0, 'ansible-tmp-1627051139.35-3751-266795384375758=/tmp/ansible-tmp-1627051139.35-3751-266795384375758\n', '')
2021-07-23 10:38:59,400 p=3751 u=meggleston n=ansible | <hqd-dbora-n2> ESTABLISH SSH CONNECTION FOR USER: None
2021-07-23 10:38:59,401 p=3751 u=meggleston n=ansible | <hqd-dbora-n2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/meggleston/.ansible/cp/7058214ea1 hqd-dbora-n2 '/bin/sh -c '"'"'rm -f -r /tmp/ansible-tmp-1627051139.35-3751-266795384375758/ > /dev/null 2>&1 && sleep 0'"'"''
2021-07-23 10:38:59,444 p=3751 u=meggleston n=ansible | <hqd-dbora-n2> (0, '', '')
2021-07-23 10:38:59,449 p=3699 u=meggleston n=ansible | fatal: [hqd-dbora-n2]: FAILED! => {
    "changed": false,
    "msg": "Could not find or access '/tmp/CarbonBlack-install/CarbonBlackLinuxDevelopment-210721.tar.gz' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"
}
2021-07-23 10:38:59,450 p=3699 u=meggleston n=ansible | PLAY RECAP ********************************************************************************************************************************************
2021-07-23 10:38:59,451 p=3699 u=meggleston n=ansible | hqd-dbora-n2               : ok=4    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Output of the directory showing the file is there:
[meggleston@hq-mgtlinux-s1 ~]$ ssh hqd-dbora-n2 ls -l /tmp /tmp/CarbonBlack-install /tmp/CarbonBlack-install/CarbonBlackLinuxDevelopment-210721.tar.gz ; date
-rw-r--r--.  1 root root 52643490 Jul 23 09:50 /tmp/CarbonBlack-install/CarbonBlackLinuxDevelopment-210721.tar.gz
 
/tmp:
total 0
drwxr-xr-x. 2 root       root       55 Jul 23 09:50 CarbonBlack-install
 
/tmp/CarbonBlack-install:
total 51412
-rw-r--r--. 1 root root 52643490 Jul 23 09:50 CarbonBlackLinuxDevelopment-210721.tar.gz
Fri Jul 23 10:42:39 EDT 2021

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f45b64d-0184-2c37-bf7f-1b6170f73b2b%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Jul 23, 2021, 11:01:48 AM7/23/21
to ansible...@googlegroups.com
On 23/07/2021 16:53, Mike Eggleston wrote:
> I looked for a “tar” module; I had forgotten about “unarchive”. I have changed my playbook and the relevant changed sections are below. I still get an error:
>

"msg": "Could not find or access '/tmp/CarbonBlack-install/CarbonBlackLinuxDevelopment-210721.tar.gz' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"

What part of this error message is unclear?

Regards
Racke
>> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f45b64d-0184-2c37-bf7f-1b6170f73b2b%40linuxia.de <https://groups.google.com/d/msgid/ansible-project/7f45b64d-0184-2c37-bf7f-1b6170f73b2b%40linuxia.de>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/22697C31-36B9-456D-8CEF-1356D3EE384A%40gmail.com <https://groups.google.com/d/msgid/ansible-project/22697C31-36B9-456D-8CEF-1356D3EE384A%40gmail.com?utm_medium=email&utm_source=footer>.

Mike Eggleston

unread,
Jul 23, 2021, 11:04:40 AM7/23/21
to ansible...@googlegroups.com
Below the error message I showed where the file does exist on the remote host.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0955e528-8dda-b440-4bfb-5b7e8f39af38%40linuxia.de.

Mike Eggleston

unread,
Jul 23, 2021, 11:12:25 AM7/23/21
to ansible...@googlegroups.com
The file is on the remote host, as shown in the output below the error message, but the error message does say the file for “unarchive” "on the Ansible Controller”. Why is “unarchive” module looking on the Ansible controller for the file?

Stefan Hornburg (Racke)

unread,
Jul 23, 2021, 11:17:04 AM7/23/21
to ansible...@googlegroups.com
On 23/07/2021 17:12, Mike Eggleston wrote:
> The file is on the remote host, as shown in the output below the error message, but the error message does say the file for “unarchive” "on the Ansible Controller”. Why is “unarchive” module looking on the Ansible controller for the file?

It is all in the error message:

expect the file to exist on the remote, see the remote_src option

Works the same way as "copy" module does.

Regards
Racke

Mike Eggleston

unread,
Jul 23, 2021, 11:22:22 AM7/23/21
to ansible...@googlegroups.com
Duh. Thank you. :)
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2df18b61-61dd-8e47-c234-6bf196d51f0e%40linuxia.de.

Mike Eggleston

unread,
Jul 23, 2021, 1:41:52 PM7/23/21
to ansible...@googlegroups.com
Well, I changed things around and used the shell module rather than the command module. I put “cd {{installdir}} ; sh {{script}}” in the playbook. The full playbook is here:

---
- hosts: linux
  gather_facts: yes
  become: yes
 
  vars:
    file: "CarbonBlackLinuxInstaller-v6.3.4.10012-Default-Group-210309.tar.gz"
    installdir: "/tmp/CarbonBlack-install"
    description: "install and start the Carbon Black Reponse agent"
 
  tasks:
    - set_fact:
        file="CarbonBlackLinuxDevelopment-210721.tar.gz"
        script="b9install.sh"
      when: (ansible_host is match("hqd-") or ansible_host is match("unix-dev-") or ansible_host is match(".dev") or ansible_all_ipv4_addresses[0] is match ("192.168.60.")) == true
 
    - name: "create {{installdir}}"
      file:
        path="{{installdir}}"
        mode="0755"
        state="directory"
 
    - name: "copy {{file}} to the {{ansible_host}}"
      copy:
        src="../templates/{{file}}"
        dest="{{installdir}}/{{file}}"
        owner="root"
        group="root"
        mode="0644"
        backup="yes"
 
    - name: "extract {{file}}"
      unarchive:
        src="../templates/{{file}}"
       dest="{{installdir}}"
 
    - name: "run {{script}}"
      shell: "cd {{installdir}} ; sh {{script}}"
      register: x
 
    #- debug: msg="pwd='{{x}}'"
 
    #- fail: msg="doing this for testing"
 
    - name: "delete {{installdir}}"
      file:
        path: "{{installdir}}"
        state: absent
 
    - name: "update /ChangeLog - {{description}}"
      lineinfile:
        path: /ChangeLog
        regexp: "^changeme"
        line: "{{ansible_date_time.iso8601}} {{lookup('env', 'USER') | default('UNKNOWN', true)}} {{description}}"
        insertafter: EOF
        create: yes
        mode: 0644
        backup: no

I just realized I don’t need “copy” and “unarchive”, so I’ll remove “copy” and test again.
Reply all
Reply to author
Forward
0 new messages