commandまたはshellの結果を変数に設定する
command の register で変数にコマンドの結果を設定することができる。
code:playbook.yml
- name: Sample command result
hosts: localhost
gather_facts: no
tasks:
- name: Sample command result
command: whoami
register: var1
changed_when: false
- name: echo var1
debug:
msg: "{{ var1 }}"
結果
code:console
$ ansible-playbook command_register.yml
WARNING: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY Sample command result ************************************************************************************************************************************************************************************** TASK Sample command result ************************************************************************************************************************************************************************************** TASK echo var1 ************************************************************************************************************************************************************************************************** "msg": {
"changed": false,
"cmd": [
"whoami"
],
"delta": "0:00:00.005717",
"end": "2021-06-15 16:03:34.403357",
"failed": false,
"rc": 0,
"start": "2021-06-15 16:03:34.397640",
"stderr": "",
"stderr_lines": [],
"stdout": "ubuntu",
"stdout_lines": [
"ubuntu"
]
}
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0