Answers: <blockquote>
    <p>Q: <em>Is it possible to have a pair of IPs in Ansible?</em></p>
  </blockquote>
  
  <p>A: Yes. It is possible. For example, it might be useful to create groups in the inventory with all pieces of information.</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>It's possible to simplify the fetching of the configuration.</p>
  
  <pre><code>- hosts: primary, standby
    remote_user: root
    tasks:
    - name: copy the config to local
      fetch:
        src: /data1/cronjobs/gen_ddl
        dest: /AnsibleDir/conf
  </code></pre>
  
  <p>Without <code>flat: yes</code> the configuration files will be stored in</p>
  
  <pre><code>/AnsibleDir/conf/{{ inventory_hostname }}/data1/cronjobs/gen_ddl
  </code></pre>
  
  <p>Then it's possible to reference the configuration files by the inventory hostname. For example <code>groups['primary']</code> (<code>secondary</code> the same way). Take a look at <code>myshell_output</code> and select the data for <code>content</code> and <code>dest</code> from the <code>items</code></p>
  
  <pre><code>- 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: "{{ &lt;SELECT-FROM-ITEM&gt; }}"
        dest: "/AnsibleDir/output.{{ &lt;SELECT-FROM-ITEM&gt; }}"
      loop: "{{ myshell_output.results }}"
  </code></pre>
  
  <p>To reference primary/standby pairs use group <code>ha</code>. For example the play below changes the configuration both at primary and standby for all hosts in <code>ha</code>.</p>
  
  <pre><code>- hosts: ha
    tasks:
      - name: Change configuration at primary
        lineinfile:
          path: /AnsibleDir/conf/{{ inventory_hostname }}/data1/cronjobs/gen_dd
          regexp: &lt;ADD-REGEXP&gt;
          line: &lt;ADD-LINE&gt;
      - name: Change configuration at standby
        lineinfile:
          path: /AnsibleDir/conf/{{ hostvars[inventory_hostname].stdby }}/data1/cronjobs/gen_dd
          regexp: &lt;ADD-REGEXP&gt;
          line: &lt;ADD-LINE&gt;
  </code></pre>
  
  <blockquote>
    <p>Q: <em>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.</em></p>
  </blockquote>
  
  <p>A: Fix the configuration files in 2 machines</p>
  
  <pre><code>- 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"
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-03T05:39:27.260Z
Answers: would you please give an example about this `ha`? how i can specify each on in tasks? where to use `stdby`?
Answers: BlackCrystal
Answers: CC BY-SA 4.0
Answers: 2019-09-03T06:08:58.960Z
Answers: Sure. I've updated the answer.
Answers: Vladimir Botka
Answers: CC BY-SA 4.0
Answers: 2019-09-03T05:01:54.983Z
Answers: 2019-09-03T06:45:27.597Z
Answers: 2019-09-03T06:45:27.597Z
Answers: Vladimir Botka
Body: <p>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:  </p>
  
  <pre><code>- 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 }}"  
  </code></pre>
  
  <p>and I use this command:<br>
  <code>ansible-playbook -i /Ansible/hostfile /Ansible/python.yml --extra-vars "host=test1"</code>  </p>
  
  <p>in <code>hostfile</code> I have this configuration:  </p>
  
  <pre><code>[primary]
  192.168.107.69
  [standby]
  192.168.107.70
  </code></pre>
  
  <p>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?<br>
  for example something like this:  </p>
  
  <pre><code>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  
  </code></pre>
  
  <p>or can I assign each pair the variable I want instead of writing it in my command every time? like:  </p>
  
  <pre><code>primary:1.1.1.1,standby:1.1.1.2,host:test1
  primary:1.1.1.3,standby:1.1.1.4,host:test2  
  </code></pre>
  
  <p>I searched a lot but I could't find anything and I'm new to ansible. Any suggestions? </p>
  
Comments: CC BY-SA 4.0
Comments: 2019-09-03T06:13:48.677Z
Comments: 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.
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2019-09-03T06:26:18.843Z
Comments: @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.
Comments: BlackCrystal
Comments: CC BY-SA 4.0
Comments: 2019-09-03T06:40:03.397Z
Comments: How should "the python script generated report" look like?
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2019-09-03T07:25:11.893Z
Comments: @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.
Comments: BlackCrystal
Comments: CC BY-SA 4.0
Comments: 2019-09-03T07:33:40.187Z
Comments: Make it [mcve](https://stackoverflow.com/help/minimal-reproducible-example).
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2019-09-03T09:06:01.053Z
Comments: @VladimirBotka thank you for sharing this link. i'll try to observe it in my questions. and your answer worked perfectly.
Comments: BlackCrystal
Tags: <ansible>
Title: Is it possible to have pair of IPs in Ansible?
UserDisplayName: BlackCrystal
ThreadText: # Is it possible to have pair of IPs in Ansible?
  
  (Asked by: username_0 on 2019-09-03)
  
  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?
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: @username_1  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.  
  > **username_1**: How should "the python script generated report" look like?  
  > **username_0**: @username_1  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.  
  > **username_1**: Make it [mcve](https://stackoverflow.com/help/minimal-reproducible-example).  
  > **username_0**: @username_1 thank you for sharing this link. i'll try to observe it in my questions. and your answer worked perfectly.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-09-03. Score: 2 | Accepted answer
  
  > 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"
  
  ```
  
  > **Comments:**
  > **username_0**: would you please give an example about this `ha`? how i can specify each on in tasks? where to use `stdby`?  
  > **username_1**: Sure. I've updated the answer.  
  
  ---
  
  