- Set up the hostname of ansible server to ansible.localdomain
- Install the ansible package
yum install ansible
- Add servers (master,infra,worker) to /etc hosts.
[root@ansible ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.217.10 master 192.168.217.11 infra 192.168.217.12 worker1
- Set up ansible.conf, uncomment the privilege_escalation parameters.
[privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=False
- Create a temp group to the /etc/ansible/hosts file
[temp] master infra worker1
- Use the ssh-copy-id command for cop the public key to the nodes.
[root@ansible ansible]# for i in $(cat /etc/hosts|grep 192|awk {'print $2'}); do ssh-copy-id $i; done /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@master's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'master'" and check to make sure that only the key(s) you wanted were added. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@infra's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'infra'" and check to make sure that only the key(s) you wanted were added. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@worker1's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'worker1'" and check to make sure that only the key(s) you wanted were added.
- Try it!
[root@ansible ansible]# for i in $(cat /etc/hosts|grep 192|awk {'print $2'}); do echo $i; ssh $i "ip a s |grep 192; exit;"; done master inet 192.168.217.10/24 brd 192.168.217.255 scope global ens33 infra inet 192.168.217.11/24 brd 192.168.217.255 scope global ens33 worker1 inet 192.168.217.12/24 brd 192.168.217.255 scope global ens33
- Check that ansible can reach the servers
[root@ansible ansible]# ansible -i hosts temp -m ping worker1 | SUCCESS => { "changed": false, "ping": "pong" } master | SUCCESS => { "changed": false, "ping": "pong" } infra | SUCCESS => { "changed": false, "ping": "pong" }
- Set up the hostnames og the servers with ansible
[root@ansible ansible]# ansible -i hosts master -m shell -a "hostnamectl set-hostname master.localdomain" master | SUCCESS | rc=0 >> [root@ansible ansible]# ansible -i hosts infra -m shell -a "hostnamectl set-hostname infra.localdomain" infra | SUCCESS | rc=0 >> [root@ansible ansible]# ansible -i hosts worker1 -m shell -a "hostnamectl set-hostname worker1.localdomain" worker1 | SUCCESS | rc=0 >> [root@ansible ansible]# ansible -i hosts temp -m shell -a "hostname" master | SUCCESS | rc=0 >> master.localdomain worker1 | SUCCESS | rc=0 >> worker1.localdomain infra | SUCCESS | rc=0 >> infra.localdomain
- The last command will overwrite the resolv.conf, use the following command to fix it.
[root@ansible ansible]# ansible -i hosts temp -m shell -a "echo 'DNS1=<your gateway or dns server>' >> /etc/sysconfig/network-scripts/ifcfg-ens33" master | SUCCESS | rc=0 >> infra | SUCCESS | rc=0 >> worker1 | SUCCESS | rc=0 >> [root@ansible ansible]# ansible -i hosts temp -m shell -a "systemctl restart network" master | SUCCESS | rc=0 >> worker1 | SUCCESS | rc=0 >> infra | SUCCESS | rc=0 >>
febr 02