#yum install ansible
it available in epel repository, for redhat you have to buy dedicated subscription for ansible
once installed
you have to specify our target hosts in to /etc/ansible/hosts
# vi /etc/ansible/hosts
[root@ansibleserver playbook]# ansible --list-host all
hosts (1):
172.31.50.3
[root@ansibleserver playbook]# ansible --list-host client
hosts (1):
172.31.50.3
[root@ansibleserver playbook]# ansible all -m ping
172.31.50.3 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 172.31.50.3 port 22: No route to host",
"unreachable": true
}
# ssh-keygen -t rsa
//after create the key i have copied the id_rsa.pub key and pasted into the client server /root/.ssh/authorized_keys
[root@ansibleserver playbook]# ansible client -m ping
34.x.x.x | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
//after that it's pinging
// now create the playbook
[root@ansibleserver playbook]# vi nginx-install.yml
--
- name: nginx install & start services
hosts: all
become: true
tasks:
- name: install nginx
yum:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started
//save , and this my play book to install nginx and start the service
//for validate yaml file online http://www.yamllint.com/
//before run the book make sure the service which are already running on client
[root@ansibleclient ec2-user]# yum list installed | grep nginx
//so not installed from client side
//go to the server
[root@ansibleserver playbook]# ansible-playbook -i /etc/ansible/hosts nginx-install.yml --check
PLAY [nginx install & start services] ****************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [34.x.x.x]
TASK [install nginx] *********************************************************************************************************************************
changed: [34.x.x.x.x]
TASK [start nginx] ***********************************************************************************************************************************
changed: [34.x.x.x.x]
PLAY RECAP *******************************************************************************************************************************************
34.x.x.x.x : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansibleserver playbook]#
//if all test are sucessful , then we can remove the --check and execute
[root@ansibleserver playbook]# ansible-playbook -i /etc/ansible/hosts nginx-install.yml --check
PLAY [nginx install & start services] ****************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [34.]
TASK [install nginx] *********************************************************************************************************************************
changed: [34.]
TASK [start nginx] ***********************************************************************************************************************************
changed: [34.]
PLAY RECAP *******************************************************************************************************************************************
34.2 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansibleserver playbook]# ansible-playbook -i /etc/ansible/hosts nginx-install.yml
PLAY [nginx install & start services] ****************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [34.]
TASK [install nginx] *********************************************************************************************************************************
changed: [34.]
TASK [start nginx] ***********************************************************************************************************************************
changed: [34.]
PLAY RECAP *******************************************************************************************************************************************
34. : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//now sucessfully installed.
//check from client side
[root@ansibleclient ec2-user]# yum list installed | grep nginx
nginx.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-all-modules.noarch 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-filesystem.noarch 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-http-image-filter.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-http-perl.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-http-xslt-filter.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-mail.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-stream.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
//installed
//adhoc commands using ansible
[root@ansibleserver playbook]# ansible -m shell -a "uname -a" client
[root@ansibleserver playbook]# ansible -m shell -a "hostname" client
//send files using adhoc command
[root@ansibleserver playbook]# ansible -m copy -a "src=/tmp/boobal.txt dest=/tmp/ mode=700" client
34.2| CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "91d0f879a8683f9f5626568b08f0264117e2d02c",
"dest": "/tmp/boobal.txt",
"gid": 0,
"group": "root",
"md5sum": "ca378755c55dc6b8f7f80641869d1bdf",
"mode": "0700",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 19,
"src": "/root/.ansible/tmp/ansible-tmp-1595953268.487625-3723354-124487860798885/source",
"state": "file",
"uid": 0
}
[root@ansibleserver playbook]# ansible -m shell -a "cat /tmp/boobal.txt" client
34.207.190.100 | CHANGED | rc=0 >>
hi this is my file
it available in epel repository, for redhat you have to buy dedicated subscription for ansible
once installed
you have to specify our target hosts in to /etc/ansible/hosts
# vi /etc/ansible/hosts
[root@ansibleserver playbook]# ansible --list-host all
hosts (1):
172.31.50.3
[root@ansibleserver playbook]# ansible --list-host client
hosts (1):
172.31.50.3
[root@ansibleserver playbook]# ansible all -m ping
172.31.50.3 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 172.31.50.3 port 22: No route to host",
"unreachable": true
}
# ssh-keygen -t rsa
//after create the key i have copied the id_rsa.pub key and pasted into the client server /root/.ssh/authorized_keys
[root@ansibleserver playbook]# ansible client -m ping
34.x.x.x | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
//after that it's pinging
// now create the playbook
[root@ansibleserver playbook]# vi nginx-install.yml
--
- name: nginx install & start services
hosts: all
become: true
tasks:
- name: install nginx
yum:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started
//save , and this my play book to install nginx and start the service
//for validate yaml file online http://www.yamllint.com/
//before run the book make sure the service which are already running on client
[root@ansibleclient ec2-user]# yum list installed | grep nginx
//so not installed from client side
//go to the server
[root@ansibleserver playbook]# ansible-playbook -i /etc/ansible/hosts nginx-install.yml --check
PLAY [nginx install & start services] ****************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [34.x.x.x]
TASK [install nginx] *********************************************************************************************************************************
changed: [34.x.x.x.x]
TASK [start nginx] ***********************************************************************************************************************************
changed: [34.x.x.x.x]
PLAY RECAP *******************************************************************************************************************************************
34.x.x.x.x : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansibleserver playbook]#
//if all test are sucessful , then we can remove the --check and execute
[root@ansibleserver playbook]# ansible-playbook -i /etc/ansible/hosts nginx-install.yml --check
PLAY [nginx install & start services] ****************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [34.]
TASK [install nginx] *********************************************************************************************************************************
changed: [34.]
TASK [start nginx] ***********************************************************************************************************************************
changed: [34.]
PLAY RECAP *******************************************************************************************************************************************
34.2 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansibleserver playbook]# ansible-playbook -i /etc/ansible/hosts nginx-install.yml
PLAY [nginx install & start services] ****************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [34.]
TASK [install nginx] *********************************************************************************************************************************
changed: [34.]
TASK [start nginx] ***********************************************************************************************************************************
changed: [34.]
PLAY RECAP *******************************************************************************************************************************************
34. : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//now sucessfully installed.
//check from client side
[root@ansibleclient ec2-user]# yum list installed | grep nginx
nginx.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-all-modules.noarch 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-filesystem.noarch 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-http-image-filter.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-http-perl.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-http-xslt-filter.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-mail.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
nginx-mod-stream.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @rhel-8-appstream-rhui-rpms
//installed
//adhoc commands using ansible
[root@ansibleserver playbook]# ansible -m shell -a "uname -a" client
[root@ansibleserver playbook]# ansible -m shell -a "hostname" client
//send files using adhoc command
[root@ansibleserver playbook]# ansible -m copy -a "src=/tmp/boobal.txt dest=/tmp/ mode=700" client
34.2| CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "91d0f879a8683f9f5626568b08f0264117e2d02c",
"dest": "/tmp/boobal.txt",
"gid": 0,
"group": "root",
"md5sum": "ca378755c55dc6b8f7f80641869d1bdf",
"mode": "0700",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 19,
"src": "/root/.ansible/tmp/ansible-tmp-1595953268.487625-3723354-124487860798885/source",
"state": "file",
"uid": 0
}
[root@ansibleserver playbook]# ansible -m shell -a "cat /tmp/boobal.txt" client
34.207.190.100 | CHANGED | rc=0 >>
hi this is my file
0 Comments