基于输入值的 ansible jinja2 模板文件创建

ansible jinja2 template file creation based on input values

我想创建一个 application.properties 文件并将其复制到特定位置。我正在使用模板模块。但我想根据逗号分隔的输入 ip 地址创建文件内容 count/values

文件内容如下。

conf_pg_hba_replication.connection=主机复制 oracle {{IP1}}/32 trust\nhost 复制 oracle {{IP2}}/32 trust\nhost 复制 oracle {{IP3}}/32。 ......

所以我希望根据逗号分隔的输入 ip 地址值使用动态内容创建我的文件。

如果我输入的ip值为127.0.0.1,123.123.123.123 文件内容应该是

conf_pg_hba_replication.connection=host replication oracle 127.0.0.1/32 trust\nhost replication oracle 123.123.123.123/32 trust

所以我同样需要创建文件的内容。

请帮我解决这个问题。

---
- name: pp
  hosts: localhost
  tasks:
   - name: pp
     template:
      src: pp.j2
      dest: pp.properties
      newline_sequence: \n

-bash-4.2$ cat pp.j2
conf_pg_hba_replication.connection=host    replication     oracle       {{slave_ip}}/32      trust

我通过变量将 ips 列表传递给 ansible playbook,如下所示 ansible-playbook postgres.yml -e "ips_list=ip1,ip2,ip3"

您正在将逗号分隔的 ip 列表作为 ansible 命令行上的额外变量传递。

要达到您的要求,您需要:

  1. 使用 split 函数将包含逗号分隔列表的字符串转换为真实列表。
  2. 遍历模板中的列表以输出结果。

这实际上可以在一个任务中完成。鉴于以下 templates/pp.j2...

{% for slave_ip in ip_list %}
conf_pg_hba_replication.connection=host    replication     oracle        {{ slave_ip }}/32      trust
{% endfor %}

和以下剧本...

---
- name: template our pp file
  hosts: localhost
  gather_facts: false
  tasks:
    - name: pp
      template:
        src: pp.j2
        dest: pp.properties
      vars:
        ip_list: "{{ ip_list_raw.split(',') }}"

你这样称呼...

ansible-playbook test.yml -e "ip_list_raw=127.0.0.1,1.2.3.4,5.6.7.8"

你得到以下结果

$ cat pp.properties 
conf_pg_hba_replication.connection=host    replication     oracle        127.0.0.1/32      trust
conf_pg_hba_replication.connection=host    replication     oracle        1.2.3.4/32      trust
conf_pg_hba_replication.connection=host    replication     oracle        5.6.7.8/32      trust

请注意,您还可以通过直接将 ips 列表作为 json 数组传递到命令行

,从而在模板任务中删除带有 split 的 vars 声明
ansible-playbook test.yml -e '{"ip_list":["127.0.0.1","1.2.3.4","5.6.7.8"]}'

或者甚至从外部 yaml 文件加载它们,例如my_ip_list.yml

---
ip_list:
  - 127.0.0.1
  - 1.2.3.4
  - 5.6.7.8

像这样:

ansible-playbook test.yml -e @my_ip_list.yml'
---
     - name: check for postgresql standby
       command: "{{ service }} postgres-check-standby"
       ignore_errors: yes
       register: check_standby
     - set_fact:
         is_standby: "N"
       when:
          - '"Error: Cannot get status of postgres server" in check_standby.stdout or "Error: Postgres-Server reports being" in check_standby.stdout'
          - '" pg_last_xlog_receive_location \n-------------------------------\n \n(1 row)" in check_standby.stdout'
     - set_fact:
         is_standby: "Y"
       when:
          - '"postgres-server is slave/standby" in check_standby.stdout or "Error: Cannot get status of postgres server" in check_standby.stdout'
          - '" pg_last_xlog_receive_location \n-------------------------------\n \n(1 row)" not in check_standby.stdout'
     - set_fact:
         is_standby: "D"
       when:
          - '"ERROR: postgres server is down or does not seem be standby" in check_standby.stdout'
          - '"Is the server running locally and accepting" in check_standby.stderr'
     - name: print if standby
       command: echo "postgres-server is slave/standby"
       when: is_standby == "Y"
     - name: print if not standby
       command: echo "postgres-server is not slave/standby"
       when: is_standby == "N"
     - name: print if component is down
       command: echo "postgresql component is down"
       when: is_standby == "D"

---
     - name: check for postgresql master
       command: "{{ service }} postgres-check-master"
       ignore_errors: yes
       register: check_master
     - set_fact:
        is_master: "Y"
       when:
          - '" pg_current_xlog_location \n--------------------------\n \n(1 row)" not in check_master.stdout'
          - '"postgres-server is master" in check_master.stdout or "Cannot get status of postgres server" in check_master.stdout'
     - set_fact:
        is_master: "N"
       when:
          - '"postgres server is down or does not seem be" in check_master.stdout'
          - '"ERROR:  recovery is in progress" in check_master.stderr'
     - set_fact:
        is_master: "D"
       when: 
          - '"postgres server is down or does not seem be" in check_master.stdout'
          - '"Is the server running locally and accepting" in check_master.stderr'
     - name: print if master
       command: echo "postgres-server is master"
       when: is_master == "Y"
     - name: print if not master
       command: echo "postgres-server is not master"
       when: is_master == "N"
     - name: print if component is down
       command: echo "postgresql component is down"
       when: is_master == "D"

---
        - name: check postgresql status
          command: "{{ service }} status"
          ignore_errors: yes
          register: postgresql_status
        - name: start if its down
          command: "{{ service }} start"
          when:
          - 'postgresql_status is failed or postgresql_status.rc != 0 or "apigee-service: apigee-postgresql: OK" not in postgresql_status.stderr'
        - name: validate it is a standby node
          include: check_standby.yml
        - fail:
            msg: postgres-server is not standby or down - please check"
          when: is_standby != "Y"
        - name: get ip address of old_master
          shell: host {{OLD_MASTER_FQDN}} | awk -F"has address " '{print}'
          ignore_errors: yes
          register: old_master_ip
        - name: get no of octet in ip address
          shell: echo {{old_master_ip.stdout}} | sed s'/\./ /g' | wc -w
          ignore_errors: yes
          register: ip_octet_count
        - name: echo old_master_ip
          command: echo Old Master Node Ip address:- {{old_master_ip.stdout}}
          when: old_master_ip.stdout != "" and ip_octet_count.stdout == "4"
        - name: fail if unable to get ip address
          fail:
            msg: failed to get the ip address, please check and enter FQDN of old master postgres node
          when: old_master_ip.stdout == "" or ip_octet_count.stdout != "4"
        - name: Switching standby to master
          shell: "{{ service}} promote-standby-to-master {{old_master_ip.stdout}}"
        - name: validate master post swap
          include: check_master.yml
        - fail:
           msg: postgresql is not master. please check microservice logs for error details.
          when: is_master != "Y"

        - name: backup existing file 
          command: "mv {{properties_file}} {{properties_file}}_backup{{ansible_date_time.date}}"
          when: postgres_properties_file.stat.exists
        - name: download properties file from Bitbucket
          shell: "curl -k -v -u {{ username }}:{{ passwd }} {{POSTGRESQL_PROPERTIES_BB_URL}}?raw -o {{properties_file}}"
          args:
           warn: no
        - name: restart postgresql component
          command: "{{ service }} restart"
        - name: print Outcome
          command: echo "Successfully converted postgresql slave to master"

---
        - name: check whether node is master node
          include: check_master.yml
        - name: bring down node if master is up and running
          command: "{{ service }} stop"
          when: is_master != "D"
        - name: get ip address of new_master
          shell: host {{NEW_MASTER_FQDN}} | awk -F"has address " '{print}'
          ignore_errors: yes
          register: new_master_ip
        - name: get slave ip
          shell: hostname -i
          ignore_errors: yes
          register: slave_ip
        - name: get no of octet in slave ip address
          shell: echo {{slave_ip.stdout}} | sed s'/\./ /g' | wc -w
          ignore_errors: yes
          register: slave_ip_octet_count
        - name: get no of octet in new master ip address
          shell: echo {{new_master_ip.stdout}} | sed s'/\./ /g' | wc -w
          ignore_errors: yes
          register: ip_octet_count_m
        - name: echo new_master_ip and slave ip
          command: "{{ item }}"
          with_items:
           - echo New Master Node Ip address:- {{new_master_ip.stdout}}
           - echo Slave Ip address:- {{slave_ip.stdout}}
          when: new_master_ip.stdout != "" and ip_octet_count_m.stdout == "4" and slave_ip.stdout != "" and slave_ip_octet_count.stdout == "4"
        - name: fail if unable to get ip address
          fail:
            msg: failed to get the ip addresses, please check and enter FQDN of postgresql node
          when: new_master_ip.stdout == "" or ip_octet_count_m.stdout != "4" or slave_ip.stdout == "" or slave_ip_octet_count.stdout != "4"
        - name: create temp replication config file
          copy:
            dest: /tmp/setup_replication.txt
            content: |
              PG_MASTER={{new_master_ip.stdout}}
              PG_STANDBY={{slave_ip.stdout}}
            mode: 0750
        - name: comment out the conf_pg_hba_replication.connection property in postgresql properties file
          replace:
             path: "{{properties_file}}"
             regexp: 'conf_pg_hba_replication.connection='
             replace: '#conf_pg_hba_replication.connection='
             backup: yes
          when: postgres_properties_file.stat.exists
        - name: remove data folder
          file: 
            path: /opt/apigee/data/apigee-postgresql
            state: absent
        - name: perform postgresql standby replication
          shell: nohup {{service}} setup-replication-on-standby -f /tmp/setup_replication.txt >/tmp/standby_replication.log &
          register: nohup_rep
        - name: get PID of replication progress
          shell: ps -eaf|grep -i setup-replication-on-standby|grep -v grep
          ignore_errors: true
          register: PID
        - name: get PID count
          shell: ps -eaf|grep -i setup-replication-on-standby|grep -v grep|wc -l
          ignore_errors: true
          register: PID_COUNT
        - fail:
           msg: There is an issue with replication - Please check the logs.
          when: PID_COUNT.stdout == "0"
        - name: print PID of replication process
          command: "{{ item }}"
          with_items:
            - "echo {{nohup_rep.cmd}}"
            - "echo {{PID.stdout}}"