in the next blog we will start to create some simple playbooks to do the following:
1. transfer a new hosts file to the clients
2. install some packages on the clients
Transfer a new hosts file to the clients:
1. I created the hosts file that I will transfer to to the clients.
root@dcex1510ctrl001:~# cat /root/files/hosts127.0.0.1 localhost
10.11.25.66 dcex1510ctrl001
10.11.25.67 dcex1510clnt001
10.11.25.68 dcex1510clnt002
2. I created the playbook itself
root@dcex1510ctrl001:~# cat ansible/playbooks/hostsfile.yaml
- name: Ansible copy a file from the master node to the clients
hosts: client01
tasks:
- name: Copying hosts file to another server
become: true
copy:
src: /root/files/hosts
dest: /etc/hosts
owner: root
group: root
mode: 0644
3. I executed the playbook
ansible-playbook -i ansible/hosts/hosts.ini ansible/playbooks/hostsfile.yaml
Just a reminder for my hosts file
root@dcex1510ctrl001:~# cat ansible/hosts/hosts.ini[client01]
10.11.25.67 ansible_user=root ansible_password=ABCD1234;
[client02]
10.11.25.68 ansible_user=root ansible_password=ABCD1234;
[all:children]
client01
client02
4. As we need to make the same hosts file in the 2 clients, I will change the hosts in the above playbook
root@dcex1510ctrl001:~# cat ansible/playbooks/hostsfile.yaml
- name: Ansible copy a file from the master node to the clients
hosts: all
tasks:
- name: Copying hosts file to another server
become: true
copy:
src: /root/files/hosts
dest: /etc/hosts
owner: root
group: root
mode: 0644
Install and remove a package on the clients:
root@dcex1510ctrl001:/# cat /root/ansible/playbooks/apacheInstall.yaml
- name: Ansible playbook to install and remove apache package, it is used just for testing a simple playbook
hosts: all
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Remove Apache
apt:
name: apache2
state: absent
root@dcex1510ctrl001:/# ansible-playbook -i /root/ansible/hosts/hosts.ini /root/ansible/playbooks/apacheInstall.yaml
PLAY [Ansible playbook to install and remove apache package, it is used just for testing a simple playbook] ******************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [10.11.25.67]
ok: [10.11.25.68]
TASK [Install Apache] ***********************************************************************************
changed: [10.11.25.68]
changed: [10.11.25.67]
TASK [Remove Apache] ***********************************************************************************
changed: [10.11.25.68]
changed: [10.11.25.67]
PLAY RECAP ***********************************************************************************
10.11.25.67 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.11.25.68 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
No comments:
Post a Comment