I am trying to assign an output (variable) value to a file. Also, I would like to remove the space in front of the value. The initial command output is like below.
Command output:
# /usr/local/bin/consul acl bootstrap
AccessorID: 361e72451-3709-074b-ee7c-0a2bbe019db6
SecretID: 9d4f8869-506a-121e-d01d-768495af56756
Description: Bootstrap Token (Global Management)
Local: false
Create Time: 2020-09-23 12:48:58.775483049 +0000 UTC
Policies:
00000000-0000-0000-0000-000000000001 - global-management
Code:
- name: Consul acl bootstrap
shell: /usr/local/bin/consul acl bootstrap
register: bootstrap
- set_fact:
secrets_list: "{{ bootstrap.stdout_lines | map('trim') | list }}"
- debug:
msg: "{{ item | regex_search('[^:]*$') }}"
with_items: "{{ secrets_list[1] }}"
register: secret_key
- name: append the key
lineinfile:
path: /root/.bash_profile
line: 'export CONSUL_HTTP_TOKEN="{{ secret_key.stdout }}"'
Here is my desired output
export CONSUL_HTTP_TOKEN= 9d4f8869-506a-121e-d01d-768495af56756
but what I am getting is
export CONSUL_HTTP_TOKEN="{'results': [{'msg': ' 9d4f8869-506a-121e-d01d-768495af56756', 'failed': False, 'changed': False, 'item': 'SecretID: 9d4f8869-506a-121e-d01d-768495af56756, 'ansible_loop_var': 'item'}], 'msg': 'All items completed', 'changed': False}"
Any suggestions ?
Consul 1.7.3 (CHANGELOG) added a -format=json
output to all consul acl
commands. You can simplify this play by rewriting the playbook to use this JSON output.
---
- hosts: localhost
connection: local
tasks:
- name: Bootstrap Consul ACL system
command: /usr/local/bin/consul acl bootstrap -format=json
register: bootstrap
- name: Set bootstrap_secret_id fact
set_fact:
bootstrap_secret_id: "{{ (bootstrap.stdout | from_json).SecretID }}"
- debug:
msg: "export CONSUL_HTTP_TOKEN={{ bootstrap_secret_id }}"
$ ansible-playbook bootstrap-consul-acl.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [localhost]
TASK [Bootstrap Consul ACL system] *********************************************************************************************************
changed: [localhost]
TASK [Set bootstrap_secret_id fact] ********************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
"msg": "export CONSUL_HTTP_TOKEN=954ba1c0-581f-80e9-7a1f-ebb29d364ff1"
}
PLAY RECAP *********************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0