shell 为配置文件中未定义的变量追加值

cgyqldqp  于 2023-03-19  发布在  Shell
关注(0)|答案(1)|浏览(102)

我在set_fact中有一个变量值,现在我需要把这个值附加到配置文件中,但是这个配置文件来自AMI,并且配置文件中没有定义变量。
例如:行动手册.yml

- name: Get id
  set_fact:
     id: "{{ output.stdout }}"

file.config

ID =

注意:如果ID =“{{ id }}”,则可以修复此问题,但配置文件中没有此ID。我需要传递来自playbook.yml的ID值
我试过什么playbook.yml

- name: Get id
  set_fact:
     id: "{{ output.stdout }}"

- name: append value
  shell: "{{ item }}"
    - ID = "{{ id }}"

我所拥有的
file.config

ID =  
ID = 1233455

我期待的是:file.config

ID = 1233455

注意:file.config ID =〈需要从文件中删除此行或在此处追加值,不应有任何重复〉

b5buobof

b5buobof1#

简而言之:

- name: Add ID line in config file if id variable exists or remove it if not
  ansible.builtin.lineinfile:
    path: /path/to/file.config
    regexp: '^ID =.*$'
    line: "ID = {{ id | d('') }}"
    state: "{{ 'present' if id is defined else 'absent' }}"

相关问题