Ansible:要动态设置次play的连接目标和连接方法,可以使用add_host模块

如标题所示,这是备忘录。

“add_host模块用于创建新的主机和组,以便在同一剧本的后续播放中使用,这些主机和组将保存在内存中的清单中。”

更准确地说,即使不使用add_host,存储在清单中的主机和组也可以作为连接目标进行指定。
然而,例如,如果在当前play中要部署尚未确定连接目标、连接方式和实例数量等的虚拟机,并且在下一个play中想要连接它们,这时就需要对清单进行动态添加。

在内存中的库存中添加主机,并在下一次玩法中进行指定。

游戏策略书

- name: play01
  hosts: localhost
  gather_facts: false
  tasks:

    - name: groups befor add_host
      debug:
        var: groups


    - name: !unsafe 'add_host: { name: new_host, ansible_host: p8126a, ansible_python_interpreter: /usr/bin/python }'
      add_host:             # インベントリーにnew_hostを追加 
        name: new_host
        ansible_host: p8126a
        ansible_python_interpreter: /usr/bin/python
      register: r


    - name: result of add_host
      debug:
        var: r

    - name: groups after add_host
      debug:
        var: groups

    - debug:
        var: hostvars['new_host'].ansible_host

    - debug:
        var: hostvars['new_host'].ansible_python_interpreter


- name: play02
  hosts: new_host                   # <= インベントリーに追加したnew_host
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname
    - name: '"shell": "cat /etc/motd"'
      shell: cat /etc/motd
      register: r
    - debug:
        var: r.stdout_lines

实施

$ ansible-playbook -i localhost, sample1.yml

PLAY [play01] ****************************************************************************************************

TASK [groups befor add_host] *************************************************************************************
ok: [localhost] => {
    "groups": {
        "all": [
            "localhost"
        ],
        "ungrouped": [
            "localhost"
        ]
    }
}

TASK [add_host: { name: new_host, ansible_host: p8126a, ansible_python_interpreter: /usr/bin/python }] ***********
changed: [localhost]

TASK [result of add_host] ****************************************************************************************
ok: [localhost] => {
    "r": {
        "add_host": {
            "groups": [],
            "host_name": "new_host",
            "host_vars": {
                "ansible_host": "p8126a",
                "ansible_python_interpreter": "/usr/bin/python"
            }
        },
        "changed": true,
        "failed": false
    }
}

TASK [groups after add_host] *************************************************************************************
ok: [localhost] => {
    "groups": {
        "all": [
            "new_host",         # <= 追加
            "localhost"
        ],
        "ungrouped": [
            "localhost",
            "new_host"      # <= 追加
        ]
    }
}

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "hostvars['new_host'].ansible_host": "p8126a"
}

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "hostvars['new_host'].ansible_python_interpreter": "/usr/bin/python"
}

# <= 以下から別playとなる

PLAY [play02] ****************************************************************************************************

TASK [debug] *****************************************************************************************************
ok: [new_host] => {
    "inventory_hostname": "new_host"         # <= 接続先がnew_hostとなっている
}

TASK ["shell": "cat /etc/motd"] **********************************************************************************
changed: [new_host]

TASK [debug] *****************************************************************************************************
ok: [new_host] => {
    "r.stdout_lines": [
        "*******************************************************************************",
        "*                                                                             *",
        "*                                                                             *",
        "*  Welcome to AIX Version 7.2!                                                *",
        "*                                                                             *",
        "*                                                                             *",
        "*  Please see the README file in /usr/lpp/bos for information pertinent to    *",
        "*  this release of the AIX Operating System.                                  *",
        "*                                                                             *",
        "*                                                                             *",
        "*******************************************************************************"
    ]
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
new_host                   : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

添加主机组到内存中的清单,并在下一次play中进行指定。

游戏规则手册


- name: play01
  hosts: localhost
  gather_facts: false
  vars:
    add_host:                  # <= play varsに定義
      - name: new_host01
        groups: new_group   # <= group名を合わせる
        ansible_host: p8126a
        ansible_python_interpreter: /usr/bin/python
      - name: new_host02
        groups: new_group   # <= group名を合わせる
        ansible_host: p8127a
        ansible_python_interpreter: /usr/bin/python
  tasks:


    - name: 'add_host'
      loop: '{{ add_host }}'
      add_host:                # <= インベントリーにgroups指定でhost追加
        name: '{{ item.name }}'
        ansible_connection: '{{ item.ansible_connection | default(omit) }}'
        ansible_host: '{{ item.ansible_host | default(omit) }}'
        ansible_python_interpreter: '{{ item.ansible_python_interpreter | default(omit)
          }}'
        ansible_user: '{{ item.ansible_user | default(omit) }}'
        ansible_port: '{{ item.ansible_port | default(omit) }}'
        ansible_ssh_private_key_file: '{{ item.ansible_ssh_private_key_file | default(omit)
          }}'
        groups: '{{ item.groups | default(omit) }}'

    - name: groups after add_host
      debug:
        var: groups


- name: play02
  hosts: new_group        # <= インベントリーに追加したnew_group
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname
    - name: '"shell": "cat /etc/motd"'
      shell: cat /etc/motd
      register: r
    - debug:
        var: r.stdout_lines
$ ansible-playbook -i localhost, sample2.yml 

PLAY [play01] ****************************************************************************************************

TASK [add_host] ************************************************************************************
changed: [localhost] => (item={'name': 'new_host01', 'ansible_host': 'p8126a', 'ansible_python_interpreter': '/usr/bin/python', 'groups': 'new_group'})
changed: [localhost] => (item={'name': 'new_host02', 'ansible_host': 'p8127a', 'ansible_python_interpreter': '/usr/bin/python', 'groups': 'new_group'})

TASK [groups after add_host] *************************************************************************************
ok: [localhost] => {
    "groups": {
        "all": [               # <= もし以降のplayでhosts: allを指定すると現対象hostも含まれる
            "new_host01",
            "new_host02",
            "localhost"
        ],
        "new_group": [     # <= 追加ホストで指定したgroup
            "new_host01",
            "new_host02"
        ],
        "ungrouped": [
            "localhost"
        ]
    }
}

# <= 以下から別playとなる

PLAY [play02]           #〈= 表示名を明示指定しない場合、hosts:に渡される名前(この例ではnew_group)が使用される。--limit SUBSET相当。
****************************************************************************************************

TASK [debug] *****************************************************************************************************
ok: [new_host01] => {
    "inventory_hostname": "new_host01"
}
ok: [new_host02] => {
    "inventory_hostname": "new_host02"
}

TASK ["shell": "cat /etc/motd"] **********************************************************************************
changed: [new_host01]
changed: [new_host02]

TASK [debug] *****************************************************************************************************
ok: [new_host01] => {
    "r.stdout_lines": [
        "*******************************************************************************",
        "*                                                                             *",
        "*                                                                             *",
        "*  Welcome to AIX Version 7.2!                                                *",
        "*                                                                             *",
        "*                                                                             *",
        "*  Please see the README file in /usr/lpp/bos for information pertinent to    *",
        "*  this release of the AIX Operating System.                                  *",
        "*                                                                             *",
        "*                                                                             *",
        "*******************************************************************************"
    ]
}
ok: [new_host02] => {
    "r.stdout_lines": [
        "*******************************************************************************",
        "*                                                                             *",
        "*                                                                             *",
        "*  Welcome to AIX Version 7.2!                                                *",
        "*                                                                             *",
        "*                                                                             *",
        "*  Please see the README file in /usr/lpp/bos for information pertinent to    *",
        "*  this release of the AIX Operating System.                                  *",
        "*                                                                             *",
        "*                                                                             *",
        "*******************************************************************************"
    ]
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
new_host01                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
new_host02                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

不清楚

    • play varsは、インベントリーに書かなくてもplayに直接書けば良いので、重要度は低い。

 

    group varsをどう書くかは未調査。書ける?

请在中国本土制定以下原生的参考方案。

以下是答案的中文释义:
你可以在Ansible的playbook中指定inventory文件,通过指定`-i`选项来实现。下面是一个示例命令:

“`bash
ansible-playbook -i inventory_file playbook.yml
“`

其中,`inventory_file`是你要使用的inventory文件的路径,`playbook.yml`是你要运行的playbook文件的路径。

希望这对你有帮助!

 

bannerAds