获取注册 stdout_lines 并删除 space
Get register stdout_lines and remove the space
我正在尝试将输出(变量)值分配给文件。另外,我想删除值前面的 space 。初始命令输出如下。
命令输出:
# /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
代码:
- 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 }}"'
这是我想要的输出
export CONSUL_HTTP_TOKEN= 9d4f8869-506a-121e-d01d-768495af56756
但我得到的是
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}"
有什么建议吗?
我不会使用正则表达式来执行如此简单的拆分操作,尤其是知道 Python 的 split()
对于这些用例工作得很好。
鉴于剧本:
- hosts: all
gather_facts: no
vars:
bootstrap:
stdout_lines:
- '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'
tasks:
- debug:
msg: "export CONSUL_HTTP_TOKEN={{ bootstrap.stdout_lines[1].split()[1] }}"
这会产生结果:
PLAY [all] ********************************************************************************************************
TASK [debug] ******************************************************************************************************
ok: [localhost] => {
"msg": "export CONSUL_HTTP_TOKEN=9d4f8869-506a-121e-d01d-768495af56756"
}
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
更进一步,您可以 select
and match
输出正确的行。
根据剧本
- hosts: all
gather_facts: no
vars:
bootstrap:
stdout_lines:
- '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'
tasks:
- debug:
msg: "export CONSUL_HTTP_TOKEN={{ (bootstrap.stdout_lines | select('match','SecretID:') | first).split()[1] }}"
当然会进行同样的回顾。
Consul 1.7.3 (CHANGELOG) 向所有 consul acl
命令添加了 -format=json
输出。您可以通过重写剧本以使用此 JSON 输出来简化此剧本。
---
- 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
我正在尝试将输出(变量)值分配给文件。另外,我想删除值前面的 space 。初始命令输出如下。
命令输出:
# /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
代码:
- 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 }}"'
这是我想要的输出
export CONSUL_HTTP_TOKEN= 9d4f8869-506a-121e-d01d-768495af56756
但我得到的是
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}"
有什么建议吗?
我不会使用正则表达式来执行如此简单的拆分操作,尤其是知道 Python 的 split()
对于这些用例工作得很好。
鉴于剧本:
- hosts: all
gather_facts: no
vars:
bootstrap:
stdout_lines:
- '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'
tasks:
- debug:
msg: "export CONSUL_HTTP_TOKEN={{ bootstrap.stdout_lines[1].split()[1] }}"
这会产生结果:
PLAY [all] ********************************************************************************************************
TASK [debug] ******************************************************************************************************
ok: [localhost] => {
"msg": "export CONSUL_HTTP_TOKEN=9d4f8869-506a-121e-d01d-768495af56756"
}
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
更进一步,您可以 select
and match
输出正确的行。
根据剧本
- hosts: all
gather_facts: no
vars:
bootstrap:
stdout_lines:
- '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'
tasks:
- debug:
msg: "export CONSUL_HTTP_TOKEN={{ (bootstrap.stdout_lines | select('match','SecretID:') | first).split()[1] }}"
当然会进行同样的回顾。
Consul 1.7.3 (CHANGELOG) 向所有 consul acl
命令添加了 -format=json
输出。您可以通过重写剧本以使用此 JSON 输出来简化此剧本。
---
- 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