【问题标题】:Ansible - Get key pair value from file on remote machine and populate a variableAnsible - 从远程机器上的文件中获取密钥对值并填充变量
【发布时间】:2020-08-27 05:02:09
【问题描述】:

尝试从 .yaml 文件中提取密钥对值并将其填充到变量中:

/etc/puppetlabs/puppet/csr_attributes.yaml

文件示例:

extension_requests:
  pp_service: 'private'
  pp_instance_id:  'blah'
  pp_image_name:  'RedHat 7.7 Base'
  pp_project:  'TBT'
  pp_application:  'xxxxx'
  pp_environment: 'dev'
  pp_role: 'base_stuff'
  pp_software_version: '2020-04-30'
  pp_provisioner: 'Ansible Tower'
  pp_datacenter: 'DC2'
  pp_zone: 'C6600_NPE_RS'
  pp_cloudplatform: 'esx'
  pp_apptier: 'dev'
  pp_securitypolicy: 'Stuff'
  1.3.6.1.4.1.34380.1.2.1:  ''
  1.3.6.1.4.1.34380.1.2.2:  '8'
  1.3.6.1.4.1.34380.1.2.3:  '77504'

我可以通过行号来实现,但我需要它更加动态,因为服务器之间的行不同:

当前单行代码:

- name: cat file
  shell: cat /etc/puppetlabs/puppet/csr_attributes.yaml
  register: cat_content_file

- set_fact:
    pp_service: "pp_service: {{ cat_content_file.stdout_lines[2].split()[1] }}"
    pp_application: "pp_application: {{ cat_content_file.stdout_lines[6].split()[1] }}"

- debug:
    msg:
    - "{{ pp_service }}"
    - "{{ pp_application }}"

我想我需要将输出转换为字典,但我完全不知道该怎么做。 任何建议将不胜感激。

【问题讨论】:

    标签: file dictionary ansible cat


    【解决方案1】:

    您可以使用filefrom_yaml 的组合:

      - set_fact:
          your_variable: "{{ lookup('file','/etc/puppetlabs/puppet/csr_attributes.yaml') | from_yaml }}"
      - debug: var=your_variable.extension_requests.pp_service
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      相关资源
      最近更新 更多