ansible主机列表ssh互信roles

ansibleplaybook文件:

使用将主机IP和主机名对应关系写入vars/main.yml,或者通过hosts文件引入

roles/all_ssh_key/
├── tasks
│   └── main.yml
└── vars
  └── main.yml

tasks/main.yml

 1 - name: add hosts list
 2   tags: ah
 3   shell: "/usr/bin/hostnamectl  set-hostname {{ hostlists[inventory_hostname] }}"
 4 #  with_dict: "{{ hostlists }}"
 5   when: ansible_hostname !=  hostlists[inventory_hostname]
 6 
 7 - name: add hosts list
 8   tags: ahf
 9   lineinfile:
10     path: /etc/hosts
11     line: "{{item.key}} {{ item.value }}"
12   with_dict: "{{ hostlists }}"
13 
14 - name: Ensure ssh_config is set to KeyChecking no
15   lineinfile:
16     path: /etc/ssh/ssh_config
17     regexp: '^.*StrictHostKeyChecking.*$'
18     line: '   StrictHostKeyChecking no'
19 
20 - name: generating public/private rsa key pair  #生成公钥和私钥
21   shell: echo "y"|ssh-keygen -t rsa -b 2048 -N ''  -f /root/.ssh/id_rsa
22 
23 - name: Specifying a path directly
24   tags: fe
25   fetch:
26     src: ~/.ssh/id_rsa.pub
27     dest: /tmp/id_pub/id_pub-{{ inventory_hostname }}
28     flat: yes
29 
30 - name: generating public/private rsa key pair  #生成公钥和私钥
31   tags: fe
32   shell: find /tmp/id_pub/ -name 'id_pub-*' -type f -exec cat {} >/tmp/id_pub/authorized_keys.log ;
33   run_once: true
34   delegate_to: localhost
35 
36 
37 - name: Set up multiple authorized keys
38   tags: fe
39   authorized_key:
40     user: root
41     state: present
42     key:  "{{ lookup('file', '/tmp/id_pub/authorized_keys.log') }}"

vars/main.yml

#host lists
#host_ip  host_name

hostlists:
  192.168.3.102: kylin-v10-01
  192.168.3.103: kylin-v10-02
  192.168.3.104: kylin-v10-03
  192.168.3.105: kylin-v10-04
原文地址:https://www.cnblogs.com/jonyq/p/14549919.html