Software >> Automation >> Ansible >> Modules >> local_action >> Examples

 

local_action


## DOCUMENTATION

online help: https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html



## EXAMPLE 1
## PLAYBOOK

---
- name: local_action module example 1
  hosts: all
  become: True
  tasks:
  - name: Run shell command to gather info on managed host
    shell: grep PermitRootLogin /etc/ssh/sshd_config
    register: result1
  - name: use local_action module to save result in the control node using results of the managed node
    local_action: copy content="{{ result1.stdout }}" dest=/tmp/{{ ansible_hostname }}_info.txt

## OUTPUT

PLAY [local_action module example 1] ********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************
ok: [mgmt7_int]
ok: [rhel6_int]
ok: [debian11_int]
ok: [mgmt8_int]
ok: [rhel7_int]
ok: [rhel8_int]
ok: [ubuntu20_int]

TASK [Run shell command to gather info on managed host] *************************************************************************************
changed: [rhel6_int]
changed: [mgmt7_int]
changed: [debian11_int]
changed: [rhel7_int]
changed: [ubuntu20_int]
changed: [mgmt8_int]
changed: [rhel8_int]

TASK [use local_action module to save result in the control node using results of the managed node] *****************************************
changed: [debian11_int -> localhost]
changed: [mgmt7_int -> localhost]
changed: [rhel6_int -> localhost]
changed: [mgmt8_int -> localhost]
changed: [rhel7_int -> localhost]
changed: [ubuntu20_int -> localhost]
changed: [rhel8_int -> localhost]

PLAY RECAP **********************************************************************************************************************************
debian11_int               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
mgmt7_int                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
mgmt8_int                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
rhel6_int                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
rhel7_int                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
rhel8_int                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu20_int               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


## VERIFICATION
## On the ansible control node

[ansible@mgmt7 ~]$ ls -lah /tmp/*info.txt
-rw-r--r--. 1 root root 87 Oct 26 18:46 /tmp/debian11_info.txt
-rw-r--r--. 1 root root 72 Oct 26 18:46 /tmp/mgmt7_info.txt
-rw-r--r--. 1 root root 72 Oct 26 18:46 /tmp/mgmt8_info.txt
-rw-r--r--. 1 root root 73 Oct 26 18:46 /tmp/rhel6_info.txt
-rw-r--r--. 1 root root 72 Oct 26 18:46 /tmp/rhel7_info.txt
-rw-r--r--. 1 root root 72 Oct 26 18:46 /tmp/rhel8_info.txt
-rw-r--r--. 1 root root 87 Oct 26 18:46 /tmp/ubuntu20_info.txt