Shell and Command Modules in Ansible

[root@localhost ~]# cat pwd.yml
- hosts: localhost
  tasks:
  - name: Ansible shell module output
    shell: echo $PWD
    register: command_result

  - name: Ansible shell register result
    debug: msg="{{ command_result }}"

  - name: Return only the shell standard output
    debug: msg="{{ command_result.stdout }}"
  - name: Ansible shell module output
    shell: echo $USER
    register: command_result

  - name: Ansible shell register result
    debug: msg="{{ command_result }}"

  - name: Return only the shell standard output
    debug: msg="{{ command_result.stdout }}"


[root@localhost ~]# ansible-playbook pwduser.yml

PLAY [localhost] ***************************************************************

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

TASK [Ansible shell module output] *********************************************
changed: [localhost]

TASK [Ansible shell register result] *******************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": "echo $PWD",
        "delta": "0:00:00.008749",
        "end": "2019-10-14 14:26:38.309449",
        "failed": false,
        "rc": 0,
        "start": "2019-10-14 14:26:38.300700",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "/root",
        "stdout_lines": [
            "/root"
        ]
    }
}

TASK [Return only the shell standard output] ***********************************
ok: [localhost] => {
    "msg": "/root"
}

TASK [Ansible shell module output] *********************************************
changed: [localhost]

TASK [Ansible shell register result] *******************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": "echo $USER",
        "delta": "0:00:00.007618",
        "end": "2019-10-14 14:26:38.891760",
        "failed": false,
        "rc": 0,
        "start": "2019-10-14 14:26:38.884142",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "root",
        "stdout_lines": [
            "root"
        ]
    }
}

TASK [Return only the shell standard output] ***********************************
ok: [localhost] => {
    "msg": "root"
}

PLAY RECAP *********************************************************************
localhost                  : ok=7    changed=2    unreachable=0    failed=0 

[root@localhost ~]#

Comments

Popular posts from this blog

ansible redhat cluster qorum qdevice

PE 3.9.0 on centos 6

gfs2 cluster