Wednesday, 21 August 2019

What is ansible become by Raj Gupta

When we dont have access to file to edit then how we can edit it by using ansible playbook


[ansible@ip-172-31-80-19 ~]$ vi lineInfile1.yml
[ansible@ip-172-31-80-19 ~]$ cat lineInfile1.yml
-
  name: This is our first play.
  hosts: all
  sudo: yes
  tasks:
  - name: "create a dummy file on webserver"
    lineinfile: dest=/etc/resolv.conf line="nameserver 8.8.8.8"
[ansible@ip-172-31-80-19 ~]$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /sbin/dhclient-script
search ec2.internal
nameserver 172.31.0.2
[ansible@ip-172-31-80-19 ~]$ ansible-playbook lineInfile1.yml -i inventory.txt


now run the below command on client now

[root@ip-172-31-85-190 ~]# cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /sbin/dhclient-script
search ec2.internal
nameserver 172.31.0.2
nameserver 8.8.8.8

------------------------------------------------------------------------------------------------------------

Same thing we can also do by using become: true and become_user: root


[ansible@ip-172-31-80-19 ~]$ vi lineInfile1.yml
[ansible@ip-172-31-80-19 ~]$ cat lineInfile1.yml
-
  name: This is our first play.
  hosts: all
  become: true
  become_user: root
  tasks:
  - name: "create a dummy file on webserver"
    lineinfile: dest=/etc/resolv.conf line="nameserver 1.1.1.1"
[ansible@ip-172-31-80-19 ~]$ ansible-playbook lineInfile1.yml -i inventory.txt

PLAY [This is our first play.] *****************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.85.190]

TASK [create a dummy file on webserver] ********************************************************************************************************************************
changed: [172.31.85.190]

PLAY RECAP *************************************************************************************************************************************************************
172.31.85.190              : ok=2    changed=1    unreachable=0    failed=0

[ansible@ip-172-31-80-19 ~]$

Now run the below command on client 

[root@ip-172-31-85-190 ~]# cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /sbin/dhclient-script
search ec2.internal
nameserver 172.31.0.2
nameserver 8.8.8.8
nameserver 1.1.1.1
[root@ip-172-31-85-190 ~]#


-------------------------------------------------------------------------------------------------------

Same thing if we want to do by using any other user other then root 

[ansible@ip-172-31-80-19 ~]$ vi lineInfile1.yml
[ansible@ip-172-31-80-19 ~]$ cat lineInfile1.yml
-
  name: This is our first play.
  hosts: all
  become: yes
  become_user: ansible
  become_method: su
  tasks:
  - name: "login username"
    command: touch /tmp/become_raj.txt
[ansible@ip-172-31-80-19 ~]$ ansible-playbook lineInfile1.yml -i inventory.txt --ask-become-pass
SUDO password:

PLAY [This is our first play.] *****************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.85.190]

TASK [login username] **************************************************************************************************************************************************
 [WARNING]: Consider using the file module with state=touch rather than running touch.  If you need to use command because file is insufficient you can add warn=False
to this command task or set command_warnings=False in ansible.cfg to get rid of this message.

changed: [172.31.85.190]

PLAY RECAP *************************************************************************************************************************************************************
172.31.85.190              : ok=2    changed=1    unreachable=0    failed=0


now run the below command on client 

[ec2-user@ip-172-31-85-190 ~]$ cd /tmp
[ec2-user@ip-172-31-85-190 tmp]$ ls
18.txt  become_raj.txt  Gupta4000.txt  Raj400.txt  Raj4.txt  Raj.txt  var
[ec2-user@ip-172-31-85-190 tmp]$

No comments:

Post a Comment