Save shell command output to ansible variable
Test playbook:
1[root@lampp lampp]# cat test.yml
2---
3- name: LAMPP Dev (MySQL/CentOS7)
4 hosts: 127.0.0.1
5 connection: local
6 become: true
7
8 tasks:
9 - name: Get the mysql root default password
10 command: awk '/temporary password/ {print $NF}' /var/log/mysqld.log
11 register: command_output
12
13 - set_fact:
14 default_mysqlroot_password: "{{ command_output.stdout }}"
15
16 ## NOTE: debug line below is only to show output in ansible-playbook run
17 - debug: msg="{{ default_mysqlroot_password }}"```
Output
1[root@lampp lampp]# ansible-playbook test.yml
2 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
3
4
5PLAY [LAMPP Dev (MySQL/CentOS7)] ****************************************************************************************************************
6
7TASK [Gathering Facts] *********************************************************************************************************************************
8ok: [127.0.0.1]
9
10TASK [Get the mysql root default password] *************************************************************************************************************
11changed: [127.0.0.1]
12
13TASK [set_fact] ****************************************************************************************************************************************
14ok: [127.0.0.1]
15
16TASK [debug] *******************************************************************************************************************************************
17ok: [127.0.0.1] => {
18 "msg": "712xa1dl"
19}
20
21PLAY RECAP *********************************************************************************************************************************************
22127.0.0.1 : ok=4 changed=1 unreachable=0 failed=0
Then store content as fact
1- set_fact:
2 string_to_echo: "{{ command_output.stdout }}"
More details in https://stackoverflow.com/questions/36059804/ansible-store-commands-stdout-in-new-variable