Hi Guys. I need a small clarification. ``` --- - name: "Testing in localserver . . .

Shankar:
Hi Guys. I need a small clarification.

---
- name: "Testing in localserver"
  hosts: test_server
  tasks:
    - name: 'Get the Hostname'
      command: echo ${HOSTNAME} >> ~/hostname_test
    - name: 'Create a test file'
      command: echo "this is a test file created by ansible" >> ~/ansible_file.txt

This is my test playbook. Following is the output I get when I run this playbook…(The ansible_user is testuser)

PLAY [Testing in localserver] ****************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [test_server]

TASK [Get the Hostname] **********************************************************************************************************************************************************
changed: [test_server]

TASK [Create a test file] ********************************************************************************************************************************************************
changed: [test_server]

PLAY RECAP ***********************************************************************************************************************************************************************
test_server                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

But when I login to the test_server, I couldn’t find the files under the home of testuser Can someone point out what I’m missing?

Al West:
can we see how you invoke the playbook?

Junaid Ahmed:
use shell module instead of command then check

Shankar:
I run the playbook with ansible-playbook playbook.ym -i inventory

Junaid Ahmed:
@Shankar did you try shell module ?

Shankar:
I did try but it tells me this, ERROR! 'shell' is not a valid attribute for a Play

Shankar:
Is this the proper way of using shell module?

---
- name: "Testing in localserver"
  hosts: test_server
  shell:
    - name: 'Get the Hostname'
      command: echo ${HOSTNAME} >> ~/hostname_test
    - name: 'Create a test file'
      command: echo "this is a test file created by ansible" >> ~/ansible_file.txt
  register: output

Junaid Ahmed:
replace command with shell, not to add shell as parent of tasks

Junaid Ahmed:

  • name: ‘Get the Hostname’
    shell: echo ${HOSTNAME} >> ~/hostname_test

Shankar:
Oh…give me a minute

Shankar:
Oh it works…Thanks @Junaid Ahmed But I wonder why the command module didn’t work

Shankar:
Similar way, Can I replace script with shell?

Junaid Ahmed:
command module is used to run simple commands it does not have redirection feature, for more complicated commands they have created a shell module which can redirect one command’s output to another

Shankar:
Oh…so what are the differences between script and shellmodules, then? A script uses a shell to run. Can I use shell in the place of script as well?

tech bot:
You have mentioned to echo the hostname to a directory in root. then how can you expect the output in ur home directory?

Shankar:
Its not a directory in root…It is the home directory of the ansible_user (testuser)

tech bot:
But, you have mentioned that its missing in ur home directory. Thats why got confused.