1

I have so many HA machines that one of them is primary and the other is standby. I have this playbook that copies a config file from primary and then from standby on local machine then use those files as input for a python script:

- hosts: primary
  remote_user: root
  tasks:
  - name: copy the config of primary to local
    fetch:
      src: /data1/cronjobs/gen_ddl
      dest: /AnsibleDir/primary
      flat: yes
- hosts: standby
  remote_user: root
  tasks:
  - name: copy the config of standby to local
    fetch:
      src: /data1/cronjobs/gen_ddl
      dest: /AnsibleDir/standby
      flat: yes
- hosts: 127.0.0.1
  connection: local
  remote_user: root
  tasks:
  - name: run python script
    shell: /AnsibleDir/driver.py
    register: myshell_output
  - name: copy the output to a local file
    copy:
      content: "{{ myshell_output.stdout }}"
      dest: "/AnsibleDir/output.{{ host }}"  

and I use this command:
ansible-playbook -i /Ansible/hostfile /Ansible/python.yml --extra-vars "host=test1"

in hostfile I have this configuration:

[primary]
192.168.107.69
[standby]
192.168.107.70

I think that I should edit this file every time I want to run my playbook. so is it possible to have so many pairs of IPs?
for example something like this:

primary:1.1.1.1,standby:1.1.1.2
primary:1.1.1.3,standby:1.1.1.4
primary:1.1.1.5,standby:1.1.1.6  

or can I assign each pair the variable I want instead of writing it in my command every time? like:

primary:1.1.1.1,standby:1.1.1.2,host:test1
primary:1.1.1.3,standby:1.1.1.4,host:test2  

I searched a lot but I could't find anything and I'm new to ansible. Any suggestions?

6
  • Would it be possible to describe the use-case? For example "Make equivalent changes to the configuration files of primary and standby". Then show the code you have and describe potential problems. Sep 3, 2019 at 6:13
  • @VladimirBotka actually i need to find the differences of configuration files in 2 machines. they need to be the same. in python script it will find the differences and fix them. so i need to have both configuration on my local then run the python script for them. my problem is that there are hundreds of HA machines and i needed a dynamic inventory. the python script generates a report. i needed this report with a unique name for each pair. Sep 3, 2019 at 6:26
  • How should "the python script generated report" look like? Sep 3, 2019 at 6:40
  • @VladimirBotka a unique name that refer to pair. for example the 1.1.1.1 and 1.1.1.2 report is like output.accounting. or 1.1.1.3 and 1.1.1.4 is like output.portal. Sep 3, 2019 at 7:25
  • Make it mcve. Sep 3, 2019 at 7:33

1 Answer 1

2

Q: Is it possible to have a pair of IPs in Ansible?

A: Yes. It is possible. For example, it might be useful to create groups in the inventory with all pieces of information.

[primary]
1.1.1.1
1.1.1.3
1.1.1.5

[standby]
1.1.1.2
1.1.1.4
1.1.1.6

[ha]
1.1.1.1 stdby=1.1.1.2
1.1.1.3 stdby=1.1.1.4
1.1.1.5 stdby=1.1.1.6

It's possible to simplify the fetching of the configuration.

- hosts: primary, standby
  remote_user: root
  tasks:
  - name: copy the config to local
    fetch:
      src: /data1/cronjobs/gen_ddl
      dest: /AnsibleDir/conf

Without flat: yes the configuration files will be stored in

/AnsibleDir/conf/{{ inventory_hostname }}/data1/cronjobs/gen_ddl

Then it's possible to reference the configuration files by the inventory hostname. For example groups['primary'] (secondary the same way). Take a look at myshell_output and select the data for content and dest from the items

- hosts: 127.0.0.1
  connection: local
  remote_user: root
  tasks:
  - name: loop primary config
    command: "/AnsibleDir/driver.py
              /AnsibleDir/conf/{{ item }}/data1/cronjobs/gen_ddl"
    register: myshell_output
    loop: "{{ groups['primary'] }}"
  - name: copy the output to a local file
    copy:
      content: "{{ <SELECT-FROM-ITEM> }}"
      dest: "/AnsibleDir/output.{{ <SELECT-FROM-ITEM> }}"
    loop: "{{ myshell_output.results }}"

To reference primary/standby pairs use group ha. For example the play below changes the configuration both at primary and standby for all hosts in ha.

- hosts: ha
  tasks:
    - name: Change configuration at primary
      lineinfile:
        path: /AnsibleDir/conf/{{ inventory_hostname }}/data1/cronjobs/gen_dd
        regexp: <ADD-REGEXP>
        line: <ADD-LINE>
    - name: Change configuration at standby
      lineinfile:
        path: /AnsibleDir/conf/{{ hostvars[inventory_hostname].stdby }}/data1/cronjobs/gen_dd
        regexp: <ADD-REGEXP>
        line: <ADD-LINE>

Q: I need to find the differences of configuration files in 2 machines. they need to be the same. In python script, it will find the differences and fix them. So I need to have both configurations on my local then run the python script for them.

A: Fix the configuration files in 2 machines

- hosts: ha
  tasks:
    - name: Fix primary and standby config
      command: "/AnsibleDir/driver.py
                /AnsibleDir/conf/{{ inventory_hostname }}/data1/cronjobs/gen_ddl
                /AnsibleDir/conf/{{ hostvars[inventory_hostname].stdby }}/data1/cronjobs/gen_ddl"
2
  • would you please give an example about this ha? how i can specify each on in tasks? where to use stdby? Sep 3, 2019 at 5:39
  • Sure. I've updated the answer. Sep 3, 2019 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.