Как изменить play_hosts из playbook - PullRequest
0 голосов
/ 29 мая 2020

У меня есть файл с двумя плейбуками. Инвентарь создается динамически, и нет возможности изменить его перед запуском playbook.

Выполнить с командой:

ansible-playbook -b adapter.yml --limit=host_group

adapter.yml

- name: Prepare stage
  hosts: all
  # The problem is that the inventory contains hosts in the format "x.x.x.x" ie physical address.
  # I need to run a third-party role.
  # But, it needs hosts in the format "instance-alias", that is, the name of the instance.

  tasks:
  # for this I create a new host group
  - name: Add host in new format
    add_host:
      name: "{{ item.alias }}"
      host: "{{ item.ansible_host }}"
      groups: new_format_hosts
    with_items: "{{ groups.all }}"

 # I create a new play host group that matches the previous one in a new format
  - name: Compose new play_hosts group
    add_host:
      name: "{{ item.alias }}"
      groups: new_play_hosts
    when: item.ansible_host in play_hosts
    with_items: "{{ groups.all }}"

- name: Management stage
  hosts: new_format_hosts
  # in this playbook I want to change the composition
  # of the target hosts and launch an external role
  vars:
    hostvars: "{{ hostvars }}"
    play_hosts: "{{ groups.new_play_hosts }}" # THIS DONT WORK

  - name: Run external role
    import_role:
      name: role_name
      tasks_from: file_name

Но я могу Не меняем play_hosts, чтобы запущенная роль использовала только новые хосты.

Как это исправить?

1 Ответ

0 голосов
/ 30 мая 2020

Это работает для меня:

- name: Prepare stage
  hosts: localhost

  tasks:
  - name: Show hostavers
    debug:
      msg: "{{ hostvars[item]['ansible_host'] }}"
    with_items: "{{ groups.all }}"

  # for this I create a new host group
  - name: Add host in new format
    add_host:
      name: "{{ hostvars[item].alias }}"
      ansible_host: "{{ hostvars[item].ansible_host }}"
      groups: new_format_hosts
    with_items: "{{ groups.all }}"

- name: Management stage
  hosts: new_format_hosts
  tasks:
  - name: Ping New Format Hosts
    ping:

  - name: Show ansible_host for each host
    debug:
      var: ansible_host

  - name: Show playhosts
    debug:
      var: play_hosts
    delegate_to: localhost
    run_once: yes

Это предполагает, конечно, что alias и ansible_host установлены для всех хостов.

Хосты были:

AnsibleTower ansible_host=192.168.124.8 alias=fred
192.168.124.111 ansible_host=192.168.124.111 alias=barney
jaxsatB ansible_host=192.168.124.111 alias=wilma

Соответствующий вывод playbook был:

PLAY [Management stage] *****************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************
Friday 29 May 2020  18:09:26 -0400 (0:00:00.057)       0:00:01.332 ************ 
ok: [fred]
ok: [barney]
ok: [wilma]

TASK [New Format Hosts] *****************************************************************************************************************************************************************
Friday 29 May 2020  18:09:30 -0400 (0:00:04.308)       0:00:05.641 ************ 
ok: [barney]
ok: [wilma]
ok: [fred]

TASK [Show ansible_host] ****************************************************************************************************************************************************************
Friday 29 May 2020  18:09:30 -0400 (0:00:00.450)       0:00:06.091 ************ 
ok: [fred] => {
    "ansible_host": "192.168.124.8"
}
ok: [barney] => {
    "ansible_host": "192.168.124.111"
}
ok: [wilma] => {
    "ansible_host": "192.168.124.111"
}

TASK [Show playhosts] *******************************************************************************************************************************************************************
Friday 29 May 2020  18:09:30 -0400 (0:00:00.109)       0:00:06.200 ************ 
ok: [fred -> localhost] => {
    "play_hosts": [
        "fred", 
        "barney", 
        "wilma"
    ]
}

PLAY RECAP ******************************************************************************************************************************************************************************
barney                     : ok=3    changed=0    unreachable=0    failed=0   
fred                       : ok=4    changed=0    unreachable=0    failed=0   
localhost                  : ok=3    changed=1    unreachable=0    failed=0   
wilma                      : ok=3    changed=0    unreachable=0    failed=0   

Friday 29 May 2020  18:09:30 -0400 (0:00:00.030)       0:00:06.231 ************ 

=============================================================================== 

Вам не нужно устанавливать переменную play_hosts. Это задается строкой hosts: new_format_hosts во втором воспроизведении.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...