首次执行脚本时执行任务,但此后不执行

时间:2020-10-08 19:51:07

标签: ansible

背景:

我们正在为管理员提供Ansible实用程序,用于在motd文件中添加或删除评论。我们想限制对motd文件的任何直接编辑。由于可能会有以前的评论,因此我们希望保留它们。这意味着我们只解析一次文件并捕获现有注释。之后,管理员必须使用该工具添加/删除评论。直接添加到文件中的所有注释都将被丢弃。

要求:

我有这个块,只需要运行一次。并非每次执行一次,而是多次执行一次。换句话说,它应该在我们第一次执行脚本时运行,而不是在此之后运行。

方法:

为此,我定义了一个标志变量,并像0中的common_motd_qsc_flag: 0一样将其初始化为defaults/mail.yml。执行完特定任务后,我将尝试像1那样将变量更新为common_motd_qsc_flag: 1。在任务中,我确保仅在使用0条件时标志变量为when时执行任务。

问题:

每次脚本执行时,脚本仍在运行不应运行的任务。我了解为什么会这样。这是因为在脚本启动期间,它正在读取common_motd_qsc_flag: 0中的defaults/main.yml

问题:

是否可以在不使用common_motd_qsc_flag: 1模块的情况下更新defaults/main.yml中的lineinfile?如果这是处理此要求的丑陋方法,则也可以使用任何其他方法。

tasks / main.yml:

- name: Parse all existing comments from /etc/motd
    shell: tail --lines=+10 "{{ common_motd_qsc_motd_file }}"
    register: existing_comments
    when:
      - motd_file.stat.exists == True
      - common_motd_qsc_flag == 0 # defaults

  - name: Update flag variable
    set_fact:
      common_motd_qsc_flag: 1
    when: common_motd_qsc_flag == 0

  - name: Add existing comments to the array
    set_fact:
      common_motd_qsc_comments_array: "{{ common_motd_qsc_comments_array | union([t_existing_entry]) }}"
    loop: "{{ existing_comments.stdout_lines }}"
    when:
      - not t_existing_entry is search('Note:')
      - not t_existing_entry is search('APPTYPE:')
      - not t_existing_entry is search('Comments:')
      - t_existing_entry not in common_motd_qsc_comments_array
    vars:
      t_existing_entry: "{{ item | trim }}"

defaults / main.yml:

common_motd_qsc_flag: 0

1 个答案:

答案 0 :(得分:0)

根据您的建议,我能够使用本地事实解决此问题。非常感谢您的指导。这是工作代码:

- name: Parse all existing comments from /etc/motd
  shell: tail --lines=+10 "{{ common_motd_qsc_motd_file }}"
  register: existing_comments
  when:
    - t_common_motd_qsc_check_qsc_file.stat.exists == True
    - ansible_local['snps'] is defined
    - ansible_local['snps']['cache'] is defined
    - ansible_local['snps']['cache']['common_motd_qsc_flag'] is not defined
  changed_when: false

- name: Add existing comments to the array
  set_fact:
    common_motd_qsc_comments_array: "{{ common_motd_qsc_comments_array | union([t_existing_entry]) }}"
  loop: "{{ existing_comments.stdout_lines }}"
  when:
    - ansible_local['snps'] is defined
    - ansible_local['snps']['cache'] is defined
    - ansible_local['snps']['cache']['common_motd_qsc_flag'] is not defined
    - not t_existing_entry is search('Note:')
    - not t_existing_entry is search('APPTYPE:')
    - not t_existing_entry is search('Comments:')
    - t_existing_entry not in common_motd_qsc_comments_array
  vars:
    t_existing_entry: "{{ item | trim }}"

- name: Set common_motd_qsc_flag to facts file
  ini_file:
    dest: "/etc/ansible/facts.d/snps.fact"
    section: 'cache'  # [header]
    option: 'common_motd_qsc_flag'  # key
    value: "1"  # value

- name: Add a new comment if it does not exist
  set_fact:
    common_motd_qsc_comments_array: "{{ common_motd_qsc_comments_array | union([t_new_entry]) }}"
  loop: "{{ common_motd_qsc_add_comment }}"
  when:
    - t_new_entry not in common_motd_qsc_comments_array
    - t_new_entry|length > 0
  vars:
    t_new_entry: "{{ item | trim }}"

- name: Delete an existing comment
  set_fact:
    common_motd_qsc_comments_array: "{{ common_motd_qsc_comments_array | difference([t_new_entry]) }}"
  loop: "{{ common_motd_qsc_delete_comment }}"
  when:
    - t_new_entry in common_motd_qsc_comments_array
    - t_new_entry|length > 0
  vars:
    t_new_entry: "{{ item | trim }}"

- name: Save comments to snps.fact file
  ini_file:
    dest: "/etc/ansible/facts.d/snps.fact"
    section: 'motd'  # [header]
    option: 'common_motd_qsc_comment_array'  # key
    value: "{{ common_motd_qsc_comments_array }}"  # value