Ansible 在我的库存中循环使用物品
Ansible with items loop inside my inventory
我有一个基于 shell 命令的任务需要在本地计算机上 运行。作为命令的一部分,我需要在清单文件中的部分组上添加 IP 地址
库存文件:
[server1]
130.1.1.1
130.1.1.2
[server2]
130.1.1.3
130.1.1.4
[server3]
130.1.1.5
130.1.1.6
我需要从属于服务器 2 + 3 组的 Ips 上的本地计算机运行执行以下命令
ssh-copy-id user@<IP>
# <IP> should be 130.1.1.3 , 130.1.1.4 , 130.1.1.5 , 130.1.1.6
剧本 - 我是否遗漏了 ip
- hosts: localhost
gather_facts: no
become: yes
tasks:
- name: Generate ssh key for root user
shell: "ssh-copy-id user@{{ item }}"
run_once: True
with_items:
- server2 group
- server3 group
一言难尽shell:
- hosts: server1:server2
gather_facts: no
become: true
tasks:
- name: Push local root pub key for remote user
shell: "ssh-copy-id user@{{ inventory_hostname }}"
delegate_to: localhost
请注意,我保留了您的确切 shell 命令,这实际上是一种不好的做法,因为有一个 dedicated ansible module 来管理它。所以这可以翻译成类似的东西。
- hosts: server1:server2
gather_facts: no
become: true
tasks:
- name: Push local root pub key for remote user
authorized_key:
user: user
state: present
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
我有一个基于 shell 命令的任务需要在本地计算机上 运行。作为命令的一部分,我需要在清单文件中的部分组上添加 IP 地址
库存文件:
[server1]
130.1.1.1
130.1.1.2
[server2]
130.1.1.3
130.1.1.4
[server3]
130.1.1.5
130.1.1.6
我需要从属于服务器 2 + 3 组的 Ips 上的本地计算机运行执行以下命令
ssh-copy-id user@<IP>
# <IP> should be 130.1.1.3 , 130.1.1.4 , 130.1.1.5 , 130.1.1.6
剧本 - 我是否遗漏了 ip
- hosts: localhost
gather_facts: no
become: yes
tasks:
- name: Generate ssh key for root user
shell: "ssh-copy-id user@{{ item }}"
run_once: True
with_items:
- server2 group
- server3 group
一言难尽shell:
- hosts: server1:server2
gather_facts: no
become: true
tasks:
- name: Push local root pub key for remote user
shell: "ssh-copy-id user@{{ inventory_hostname }}"
delegate_to: localhost
请注意,我保留了您的确切 shell 命令,这实际上是一种不好的做法,因为有一个 dedicated ansible module 来管理它。所以这可以翻译成类似的东西。
- hosts: server1:server2
gather_facts: no
become: true
tasks:
- name: Push local root pub key for remote user
authorized_key:
user: user
state: present
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"