Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devops-school/3e09ab77e38a2c570061b20e69194c75 to your computer and use it in GitHub Desktop.
Save devops-school/3e09ab77e38a2c570061b20e69194c75 to your computer and use it in GitHub Desktop.
hp-ansible-classroom-june-2019

Excercise 1 - Write a ansible module using Adhoc commands.

Install httpd server and create a file in /opt/index.html with content"

<html>
<h1> Welcome to HP Ansible class </h1>
</html>

Then, copy a file from /opt/index.html to /var/www/html

Excercise 2 - Adhoc commands for next all LABS.

https://www.devopsschool.com/blog/ansible-adhoc-commands-lab-excercise-part-1/

Excercise 3 - Write a ansible module using Adhoc commands to create a user and add him into sudoers and using regular user only run "Excercise 3" all adhoc commands to remote machine.

@nandiniabbinaholal
Copy link

ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=600 state=touch"
ansible 127.0.0.1 -m copy -a "dest=/opt/index.html content='

Welcome to HP Ansible class

' "
ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/index.html"

@pitchela
Copy link

pitchela commented Jul 1, 2019

#ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=666 state=touch owner=root"
#ansible 127.0.0.1 -m lineinfile -a "path=/opt/index.html line='Welcome to HPE Ansible class July- Swetha'"

#ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html"

#ansible 127.0.0.1 -m service -a "name=httpd state=started"

View this in browser: http://10.76.143.12/

Output:

Welcome to HP Asible class Welcome to HPE Ansible class July- Swetha

Exercise Lab 3:

[root@localhost ~]# ansible all -i 10.76.137.151, -m user -a "name=deploy group=wheel" -u tester -k -K -b
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 10,
"home": "/home/deploy",
"name": "deploy",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}

  1. Write a Ansible Adhoc Commands to create a group called “deploy”
    ansible all -i 10.76.137.151, -m group -a "name=deploy state=present" -u tester -K -b -k

[root@localhost ~]# ansible all -i 10.76.137.151, -m group -a "name=deploy state=present" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1001,
"name": "deploy",
"state": "present",
"system": false
}

  1. Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “deploy” and
    with /bin/bash shell.

#ansible all -i 10.76.137.151, -m user -a "name=deploy group=deploy" -u tester -K -b -k

[root@localhost ~]# ansible all -i 10.76.137.151, -m user -a "name=deploy-user group=deploy" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1001,
"home": "/home/deploy",
"move_home": false,
"name": "deploy",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}

[root@localhost ~]# ansible all -i 10.76.137.151, -m user -a "name=deploy-user group=deploy" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/deploy-user",
"name": "deploy-user",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1002
}

On ARS:

[root@localhost ~]# id deploy
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy)

[root@localhost ~]# id deploy-user
uid=1002(deploy-user) gid=1001(deploy) groups=1001(deploy)

  1. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.
    ansible all -i 10.76.137.151, -m yum -a "name=httpd state=present" -u tester -K -b -k

Add this to /etc/yum.conf
proxy=http://web-proxy.in.hpecorp.net:8080
Otherwise below error occurs

[root@localhost ~]# ansible all -i 10.76.137.151, -m yum -a "name=httpd state=present" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
tester1
10.76.137.151 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Failure talking to yum: Cannot find a valid baseurl for repo: base/7/x86_64"
}

Before the command output on ARS is as:
[root@localhost ~]# rpm -qa | grep -i http
[root@localhost ~]#

[root@localhost ~]# ansible all -i 10.76.137.151, -m yum -a "name=httpd state=present" -u tester -K -b -k -k
SSH password:
BECOME password[defaults to SSH password]:

10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: centos-hn. viettelidc.com.vn\n * extras: centos.excellmedia.net\n * updates: centos.excellmedia.net\nResolving Dep endencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be insta lled\n--> Processing Dependency: httpd-tools = 2.4.6-89.el7.centos for package: httpd-2.4.6-89.el7.cent os.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-89.el7.centos.x86_64\n-- > Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-89.el7.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-89.el7.centos.x86_64\n--> Runnin g transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util .x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-89.el7.centos will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolu tion\n\nDependencies Resolved\n\n====================================================================== ==========\n Package Arch Version Repository Size\n========== ======================================================================\nInstalling:\n httpd x86_64 2.4.6-89.el7.centos updates 2.7 M\nInstalling for dependencies:\n apr x86_64 1.4.8-3.el7_4.1 base 103 k\n apr-util x86_64 1.5.2-6.el7 base 92 k\n httpd-tools x86_64 2.4.6-89.el7.centos updates 90 k\n mailcap noarch 2.1.41-2.el7 base 3 1 k\n\nTransaction Summary\n=========================================================================== =====\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\n Downloading packages:\n-------------------------------------------------------------------------------- \nTotal 951 kB/s | 3.0 MB 00:03 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1 .4.8-3.el7_4.1.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_6 4 2/5 \n Installing : httpd-tools-2.4.6-89.el7.centos.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-89.el7.centos.x86_64 5/5 \n Verifying : httpd-t ools-2.4.6-89.el7.centos.x86_64 1/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 2/5 \n Verifying : httpd-2.4.6-89.el7.centos.x86_64 3/5 \n Verifying : apr-1.4.8-3.el7_4.1.x86_64 4/5 \n V erifying : apr-util-1.5.2-6.el7.x86_64 5/5 \n\nInstalled:\n httpd.x8 6_64 0:2.4.6-89.el7.centos \n\nDependency Installed:\n apr. x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4. 6-89.el7.centos mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}

After the command output on ARS is as:
[root@localhost ~]# rpm -qa | grep -i http
httpd-tools-2.4.6-89.el7.centos.x86_64
httpd-2.4.6-89.el7.centos.x86_64

  1. Write a Ansible Adhoc commands to start and enable the service named “httpd”
    #ansible all -i 10.76.137.151, -m service -a "name=httpd state=started" -u tester -K -b -k
    successful message

  2. Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    ansible all -i 10.76.137.151, -m file -a "path=/var/www/html state=directory" -u tester -K -b -k
    ansible all -i 10.76.137.151, -m file -a "dest=/var/www/html/index.html mode=600 state=touch" -u tester -K -b -k
    ansible all -i 10.76.137.151, -m lineinfile -a "dest=/var/www/html/index.html line='

    Welcome to HP Ansible class

    '" -u tester -K -b -k

[root@localhost ~]# ansible all -i 10.76.137.151, -m file -a "path=/var/www/html state=directory" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/var/www/html",
"secontext": "system_u:object_r:httpd_sys_content_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}

[root@localhost ~]# ansible all -i 10.76.137.151, -m file -a "dest=/var/www/html/index.html mode=600 state=touch" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/var/www/html/index.html",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "root",
"secontext": "unconfined_u:object_r:httpd_sys_content_t:s0",
"size": 0,
"state": "file",
"uid": 0
}

[root@localhost ~]#
[root@localhost ~]# ansible all -i 10.76.137.151, -m lineinfile -a "dest=/var/www/html/index.html line='

Welcome to HP Ansible class

'" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line added"
}

[root@localhost html]# cat index.html

Welcome to HP Ansible class

[root@localhost html]# [root@localhost html]# [root@localhost html]# pwd /var/www/html
  1. Write a Ansible commands to reboot a self machine.
    ansible all -i 10.76.137.151, -m reboot -u tester -K -b -k

Answer: it worked and rebooted the remote host

  1. Write a Ansible commands to install a package called “git”, “wget”.
    [root@localhost ~]# ansible all -i 10.76.137.151, -m yum -a "name=git,wget state=present" -u tester -K -b -k
    SSH password:
    BECOME password[defaults to SSH password]:
    10.76.137.151 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
    "installed": [
    "git",
    "wget"
    ]
    },
    "msg": "",
    "rc": 0,
    "results": [
    "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: centos-hn.viettelidc.com.vn\n * extras: centos.excellmedia.net\n * updates: centos.excellmedia.net\nResolving Dependencies\n--> Running transaction check\n---> Package git.x86_64 0:1.8.3.1-20.el7 will be installed\n--> Processing Dependency: perl-Git = 1.8.3.1-20.el7 for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl >= 5.008 for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: rsync for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(warnings) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(vars) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(strict) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(lib) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(Git) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(Getopt::Long) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(File::stat) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(File::Temp) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(File::Spec) for package: git-1.8.3.1-20.el7.x86_64\n--

  2. Create a temp directory to host the repo
    ansible all -i 10.76.137.151, -m file -a "path=/tmp/test state=directory" -u tester -k -b -K

Before
[root@localhost html]# ls -lrt /tmp | grep test
[root@localhost html]# pwd
/var/www/html

[root@localhost ~]# ansible all -i 10.76.137.151, -m file -a "path=/tmp/test state=directory" -u tester -k -b -K
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}

[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ls -lrt /tmp | grep test
drwxr-xr-x. 2 root root 6 Jul 1 12:30 test

  1. Write a Ansible Adhoc commands to clone git repo. T https://github.com/scmgalaxy/ansible-role-template
    #ansible all -i 10.76.137.151, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test" -u tester -k -K -b

[root@localhost ~]# ansible all -i 10.76.137.151, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test" -u tester -k -K -b
SSH password:
BECOME password[defaults to SSH password]:
tester1

10.76.137.151 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"cmd": "/bin/git clone --origin origin https://github.com/scmgalaxy/ansible-role-template /tmp/test",
"msg": "fatal: unable to access 'https://github.com/scmgalaxy/ansible-role-template/': Failed connect to github.com:443; Connection timed out",
"rc": 128,
"stderr": "fatal: unable to access 'https://github.com/scmgalaxy/ansible-role-template/': Failed connect to github.com:443; Connection timed out\n",
"stderr_lines": [
"fatal: unable to access 'https://github.com/scmgalaxy/ansible-role-template/': Failed connect to github.com:443; Connection timed out"
],
"stdout": "Cloning into '/tmp/test'...\n",
"stdout_lines": [
"Cloning into '/tmp/test'..."
]
}

This is because of lack of connectivity due to proxy settings inhpe n/w
To try at home n/w..

@chinnigit
Copy link

ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=775 state=touch"
ansible 127.0.0.1 -m copy -a "dest=/opt/index.html content='

Welcome to HP Ansible class

'"
ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/index.html mode=775"
ansible 127.0.0.1 -m yum -a "name=httpd state=present"
ansible 127.0.0.1 -m service -a "name=httpd state=restarted"

@Ahamed-sultan
Copy link

sudo ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/"
Hi Welcome to HPE, I am Ahamed

@hemananth91
Copy link

ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=644 state=touch"

ansible 127.0.0.1 -m lineinfile -a "dest=/opt/index.html line=' \n

Welcome to HP Ansible class

\n' state=present"

ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/index.html"

@vsanjeevk
Copy link

exercise 2

sudo ansible 127.0.0.1 -m group -a "name=deploy"
sudo ansible 127.0.0.1 -m user -a "name=deploy-user group=deploy shell=/bin/bash"
sudo ansible 127.0.0.1 -m yum -a "name=httpd state=present"
sudo ansible 127.0.0.1 -m service -a "name=httpd state=started enabled=1"
sudo ansible 127.0.0.1 -m file -a "dest=/var/www/html/index.html mode=644 state=touch"
sudo ansible 127.0.0.1 -m lineinfile -a "dest=/var/www/html/index.html line='<html>  <h1> Welcome to HP Ansible class </h1> </html>' state=present"
sudo ansible 127.0.0.1 -m reboot
sudo ansible 127.0.0.1 -m yum -a "name=wget state=present"
ansible 127.0.0.1 -m git -a "clone=1 repo=https://github.com/scmgalaxy/ansible-role-template dest=./ansible-role-template"


@Ashu110-8
Copy link

Ashu110-8 commented Jul 1, 2019

2 example:
Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “group” and with /bin/bash shell.
ansible 127.0.0.1 -m user -a "name=deploy-user group=group shell=/bin/bash"
Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.
ansible 127.0.0.1 -m yum -a "name=httpd state=present"

Write a Ansible Adhoc commands to start and enable the service named “httpd”
ansible 127.0.0.1 -m service -a "name=httpd state=started"

Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.
ansible 127.0.0.1 -m copy -a "dest=/var/www/html content='new content'"

Write a Ansible commands to reboot a self machine.
ansible 127.0.0.1 -m reboot

Write a Ansible commands to install a package called “git”, “wget”.
ansible 127.0.0.1 -m yum -a "name=git"
ansible 127.0.0.1 -m yum -a "name=wget"
Write a Ansible Adhoc commands to clone git repo. thttps://github.com/scmgalaxy/ansible-role-

ansible 127.0.0.1 -m git -a "clone=1 repo=https://github.com/ansible/ansible-examples.git dest=/var/github"

@prakashjawari
Copy link

ansible 127.0.0.1 -m group -a "name=deploy"
ansible 127.0.0.1 -m user -a "name=deploy-user group=deploy shell=/bin/bash state=present"
ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=755 state=touch"
ansible 127.0.0.1 -m lineinfile -a "state=present dest=/opt/index.html line='

Welcome to HPE ansible class

'"
ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html"
ansible 127.0.0.1 -m reboot -a "reboot_timeout=600"
ansible 127.0.0.1 -m yum -a "name=httpd state=present"
ansible 127.0.0.1 -m yum -a "name=git state=present"
ansible 127.0.0.1 -m yum -a "name=wget state=present"
ansible 127.0.0.1 -m git -a "repo='https://github.com/scmgalaxy/ansible-role-template' dest=ansible-role-template"

@raghvendrasawara
Copy link

ansible 127.0.0.1 -m group -a "name=deploy state=present"
ansible 127.0.0.1 -m group -a "name=deploy-user group=deploy shell='/bin/bash' state=present"
ansible 127.0.0.1 -m yum -a "name=httpd state=present"
ansible 127.0.0.1 -m service -a "name=httpd state=started"
ansible 127.0.0.1 -m copy -a "dest=/var/www/html/index.html content='

Welcome to HPE

"
ansible 127.0.0.1 -m reboot
ansible 127.0.0.1 -m yum -a "name=git state=present"
ansible 127.0.0.1 -m yum -a "name=wget state=present"
ansible 127.0.0.1 -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template dest=/home/lab_git_repo"

@hemananth91
Copy link

  1. ansible 127.0.0.1 app -s -m group -a "name=deploy state=present"
  2. ansible 127.0.0.1 app -m user -a "name=deploy-user group=deploy createhome=yes shell=/bin/bash" -b
  3. ansible 127.0.0.1 -m yum -a "name=httpd state=present"
  4. ansible 127.0.0.1 -m service -a "name=httpd state=started"
  5. ansible 127.0.0.1 -m lineinfile -a ansible 127.0.0.1 -m lineinfile -a "dest=/tmp/test.txt line='<html> \n

    Welcome to HP Ansible class

    \n'"
    ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html"
  6. ansible multi -a "reboot"
  7. ansible 127.0.0.1 -m yum -a "name=wget state=present"
    ansible 127.0.0.1 -m yum -a "name=git state=present"
  8. ansible 127.0.0.1 -m git -a "clone=yes repo= repo.thttps://github.com/scmgalaxy/ansible-role-template"

@chinnigit
Copy link

PROBLEM_2:
ansible 127.0.0.1 -m group -a "name=deploy state=present"
ansible 127.0.0.1 -m user -a "name=deploy-user group=deploy shell=/bin/bash state=present"
ansible 127.0.0.1 -m yum -a "name=httpd state=present"
ansible 127.0.0.1 -m service -a "name=httpd state=started"
ansible 127.0.0.1 -m copy -a "dest=/var/www/html/index.html content='

Welcome to HPE

'"
ansible 127.0.0.1 -m yum -a "name=git state=present"
ansible 127.0.0.1 -m yum -a "name=wget state=present"
ansible 127.0.0.1 -m git -a "dest=/home/harish/GitRepo repo=https://github.com/scmgalaxy/ansible-role-template"

@nandiniabbinaholal
Copy link

ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/index.html"
ansible 127.0.0.1 -m group -a "name=deploy state=present"
ansible 127.0.0.1 -m user -a "name=deploy-user group=deploy state=present"
ansible 127.0.0.1 -m service -a "name=httpd state=present"
ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=600 state=touch"
ansible 127.0.0.1 -m copy -a "dest=/opt/index.html content='

Welcome to HP Ansible class

'
ansible 127.0.0.1 -m reboot
ansible 127.0.0.1 -m yum -a "name=git state=present"
ansible 127.0.0.1 -m yum -a "name=wget state=present"
ansible 127.0.0.1 -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template dest=/src/ansible-examples
separate_git_dir=/src/ansible-examples.gitstate=present"

@mail2maiya
Copy link

mail2maiya commented Jul 1, 2019

ansible 127.0.0.1 -m group -a "name=deploy state=present"
ansible 127.0.0.1 -m user -a "name=deploy-user group=deploy shell= /bin/bash"
ansible 127.0.0.1 -m yum -a "name=http state=true"
ansible 127.0.0.1 -m service -a "name=httpd state=start"
ansible 127.0.0.1 -m copy -a "dest=/opt/index.html mode=755 content="

Welcome to HP Ansible class

""
ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/"

ansible 127.0.0.1 -m command -a "name=reboot state=true"
ansible 127.0.0.1 -m yum -a "name=git,wget state=true"
ansible 127.0.01 -m git -a "clone=no repo=https://github.com/scmgalaxy/ansible-role-template"

@muralidharbm1411
Copy link

ansible 127.0.0.1 -m user -a "name=deploy-user group=deploy shell=/bin/bash"
ansible 127.0.0.1 -m yum -a "name=httpd state=latest"
ansible 127.0.0.1 -m service -a "name=httpd state=started use=auto"
ansible 127.0.0.1 -m file -a "dest=/var/html/index.html mode=755 state=touch owner=root"
ansible 127.0.0.1 -m file -a "dest=/var/www/html/index.html mode=755 state=touch owner=root"
ansible 127.0.0.1 -m lineinfile -a "path=/var/www/html/index.html line='

second excercise

'"
#ansible 127.0.0.1 -m lineinfile -a "path=/var/www/html/index.html line='

second excercise

'"'
ansible 127.0.0.1 -m reboot -a "msg=reboot agthidhe"
ansible 127.0.0.1 -m reboot -a "reboot msg=reboot agthidhe"
ansible 127.0.0.1 reboot
ansible 127.0.0.1 -a "reboot msg=reboot agthidhe"
ansible 127.0.0.1 -m git -a "repo='https://github.com/scmgalaxy/ansible-role-template' dest='/home/murali/git' separate_git_dir='/home/murali/git/scmgalaxy.git'"

@anandchoubey
Copy link

anandchoubey commented Jul 1, 2019

ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=600 state=touch"
ansible 127.0.0.1 -m lineinfile -a "dest=/opt/index.html line='

Welcome to HP Ansible class

' state=present"
ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/index.html"
ansible 127.0.0.1 -m group -a "name=hpeusers state=absent"
ansible 127.0.0.1 -m user -a "name=swathi shell=/bin/bash groups=hpeusers append=yes"
ansible 127.0.0.1 -m yum -a "name=httpd state=latest"
ansible 127.0.0.1 -m service -a "name=httpd state=started"
ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=600 state=touch"
ansible 127.0.0.1 -m lineinfile -a "dest=/opt/index.html line='

Welcome to HP Ansible class

' state=present"
ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html/index.html"
ansible 127.0.0.1 -m reboot -a "reboot_timeout=3000"
ansible 127.0.0.1 -m yum -a "name=git state=latest"
ansible 127.0.0.1 -m yum -a "name=wget state=latest"
ansible 127.0.0.1 -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template dest=/srv/checkout clone=yes update=yes"

@BhanuPrakashP
Copy link

ansible 127.0.0.1 -m group -a "name=deploy state=present"
ansible 127.0.0.1 -m user -a "name=deploy-user shell=/bin/bash state=present groups=deploy append=yes"
ansible 127.0.0.1 -m service -a "name=httpd state=started enabled=yes"
ansible 127.0.0.1 -m package -a "name=git,wget state=present"
ansible 127.0.0.1 -m reboot
ansible 127.0.0.1 -m git -a "repo='https://github.com/scmgalaxy/ansible-role-template' dest='/home/bhanu/git' separate_git_dir='/home/bhanu/git/scmgalaxy.git'"

@neetesh80
Copy link

Lab-2

1. Write a Ansible Adhoc Commands to create a group called “deploy”
sudo ansible 127.0.0.1 -m group -a "name=deploy state=present"

1. Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “deploy” and with /bin/bash shell.
sudo ansible 127.0.0.1 -m user -a "name=deploy group=deploy"

2. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.
sudo ansible 127.0.0.1 -m yum -a "name=httpd state=present"

3. Write a Ansible Adhoc commands to start and enable the service named “httpd”
sudo ansible 127.0.0.1 -m service -a "name=httpd state=started"


4. Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.
sudo ansible 127.0.0.1 -m file -a "path=/var/www/html state=directory"
sudo ansible 127.0.0.1 -m file -a "dest=/var/www/html/index.html mode=600 state=touch"
sudo ansible 127.0.0.1 -m lineinfile -a "dest=/opt/index.html line='<html> <h1> Welcome to HP Ansible class </h1>  </html> '"


5. Write a Ansible commands to reboot a self machine.
sudo ansible 127.0.0.1 -m reboot


6. Write a Ansible commands to install a package called “git”, “wget”.
sudo ansible 127.0.0.1 -m yum -a "name=git,wget state=present"

7. Write a Ansible Adhoc commands to clone git repo. T https://github.com/scmgalaxy/ansible-role-template
sudo ansible 127.0.0.1 -m git -a "dest=/home/tester repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest/tmp/test"

@muralidharbm1411
Copy link

1036 ansible 127.0.0.1 -m file -a "name=/var/www/html/index.html mode=777 state=touch owner=root" -u root -b
1037 ansible all -i 127.0.0.1,15.213.56.127 -m file -a "name=/var/www/html/index.html mode=777 state=touch owner=root" -u root -b
1038 ansible all -i 127.0.0.1,15.213.56.127 -m file -a "name=/var/html/index1234.html mode=755 state=touch owner=duser" -u root -b
1039 ansible all -i 127.0.0.1,15.213.56.127 -m file -a "name=/var/www/html/index1234.html mode=755 state=touch owner=duser" -u root -b
1040 ll /var/www/html/index1234.html
1041 #ansible all -i 127.0.0.1,15.213.56.127 -m file -a "name=/var/www/html/index1234.html mode=755 state=touch owner=duser" -u root -b
1042 ansible all -i 127.0.0.1,15.213.56.127 -m lineinfile -a "path=/var/www/html/index.html line='

third excersize

'"

@raghvendrasawara
Copy link

ansible all -i 10.76.137.86, -m group -a "name=deploy state=present" -u root -k
ansible all -i 10.76.137.86, -m user -a "name=deploy-user group=deploy shell='/bin/bash' state=present" -u root -k
ansible all -i 10.76.137.86, -m yum -a "name=httpd state=present" -u root -k
ansible all -i 10.76.137.86, -m service -a "name=httpd state=started" -u root -k
ansible all -i 10.76.137.86, -m copy -a "dest=/var/www/html/index.html content='

Welcome to HPE

'" -u root -k
ansible all -i 10.76.137.86, -m reboot -u root -k
ansible all -i 10.76.137.86, -m yum -a "name=git state=present" -u root -k
ansible all -i 10.76.137.86, -m yum -a "name=wget state=present" -u root -k
ansible all -i 10.76.137.86, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template dest=/home/lab_git_repo" -u root -k

@Ashu110-8
Copy link

ansible all -i 192.168.43.223, -m user -a "name=test shell=/bin/bash password=test" -u root -k
ansible all -i 192.168.43.223, -m lineinfile -a "path=/etc/sudoers regexp='^test' line='test ALL=(ALL) NOPASSWD: ALL'" -u root -k

ansible all -i 192.168.43.223, -m group -a "name=deploy state=present " -u test -b -k
ansible all -i 192.168.43.223, -m user -a "name=deploy-user group=group shell=/bin/bash" -u test -b -k
ansible all -i 192.168.43.223, -m yum -a "name=httpd state=present" -u test -b -k
ansible all -i 192.168.43.223, -m service -a "name=httpd state=started" -u test -b -k
ansible all -i 192.168.43.223, -m copy -a "dest=/var/www/html/index.html content='new content'" -u test -b -k
ansible all -i 192.168.43.223, -m reboot -u test -b -k

ansible all -i 192.168.43.223, -m yum -a "name=git" -u test -b -k
ansible all -i 192.168.43.223, -m yum -a "name=wget" -u test -b -k
ansible all -i 192.168.43.223, -m git -a "repo=https://github.com/ansible/ansible-examples.git dest=/var/github" -u test -b -k

@vsanjeevk
Copy link

exercise 3

sudo  ansible all -i 192.168.11.129, -m user -a "name=tester group=wheel password='$1$xyz$uUyjutWOci1QzJxu496qP/'" --ask-pass
ansible all -i 192.168.11.129, -m yum -a "name=httpd state=present" -bK --ask-pass -u tester
ansible all -i 192.168.11.129, -m service -a "name=httpd state=started enabled=1" -u tester --ask-pass -bK
ansible all -i 192.168.11.129, -u tester --ask-pass -bK -m file -a "dest=/var/www/html/index.html mode=644 state=touch"
ansible all -i 192.168.11.129, -u tester --ask-pass -bK -m lineinfile -a "dest=/var/www/html/index.html line='<html>  <h1> Welcome to HP Ansible class </h1> </html>' state=present"

@neetesh80
Copy link

Lab-3 - On the Remote Machine

1. Create a user on the remote machine and add the user to the sudoers group
ansible all -i 192.168.59.163, -m user -a "name=deploy group=wheel" -u tester -K -b

2. Write a Ansible Adhoc Commands to create a group called “deploy”
ansible all -i 192.168.59.163, -m group -a "name=deploy state=present" -u tester -K -b


3. Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “deploy” and with /bin/bash shell.
ansible all -i 192.168.59.163, -m user -a "name=deploy group=deploy" -u tester -K -b

4. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.
ansible all -i 192.168.59.163, -m yum -a "name=httpd state=present" -u tester -K -b

5. Write a Ansible Adhoc commands to start and enable the service named “httpd”
ansible all -i 192.168.59.163, -m service -a "name=httpd state=started" -u tester -K -b


6. Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.
ansible all -i 192.168.59.163, -m file -a "path=/var/www/html state=directory" -u tester -K -b
ansible all -i 192.168.59.163, -m file -a "dest=/var/www/html/index.html mode=600 state=touch" -u tester -K -b
ansible all -i 192.168.59.163, -m lineinfile -a "dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible class </h1>  </html> '" -u tester -K -b

7. Write a Ansible commands to reboot a self machine.
ansible all -i 192.168.59.163, -m reboot  -u tester -K -b

8. Write a Ansible commands to install a package called “git”, “wget”.
ansible all -i 192.168.59.163, -m yum -a "name=git,wget state=present"

9. Create a temp directory to host the repo
ansible all -i 192.168.59.163, -m file -a "path=/tmp/test state=directory"

10. Write a Ansible Adhoc commands to clone git repo. T https://github.com/scmgalaxy/ansible-role-template
ansible all -i 192.168.59.163, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test"

@chinnigit
Copy link

PROBLEM 3:
#ansible all -i 10.76.136.176, -m group -a "name=deploy state=present" -u root -k
#ansible all -i 10.76.136.176, -m user -a "name=deploy-user group=deploy shell=/bin/bash state=present" -u root -k
#ansible all -i 10.76.136.176, -m yum -a "name=httpd state=present" -u root -k
#ansible all -i 10.76.136.176, -m service -a "name=httpd state=started" -u root -k
#ansible all -i 10.76.136.176, -m copy -a "dest=/var/www/html/index.html content='

Welcome to HPE

'" -u root -k
#ansible all -i 10.76.136.176, -m yum -a "name=git state=present" -u root -k
#ansible all -i 10.76.136.176, -m yum -a "name=wget state=present" -u root -k
#ansible all -i 10.76.136.176, -m git -a "dest=/home/harish/GitRepo repo=https://github.com/scmgalaxy/ansible-role-template" -u root -k

@hemananth91
Copy link

ansible all -i 192.168.5.131, -m group -a "name=deploy state=present" -u root -k
ansible all -i 192.168.5.131, -m user -a "name=deploy-user group=deploy shell='/bin/bash' state=present" -u root -k
ansible all -i 192.168.5.131, -m yum -a "name=httpd state=present" -u root -k
ansible all -i 192.168.5.131, -m service -a "name=httpd state=started" -u root -k
ansible all -i 192.168.5.131, -m copy -a "dest=/var/www/html/index.html content='

Welcome to HPE
'" -u root -k
ansible all -i 192.168.5.131, -m reboot -u root -k
ansible all -i 192.168.5.131, -m yum -a "name=git state=present" -u root -k
ansible all -i 192.168.5.131, -m yum -a "name=wget state=present" -u root -k
ansible all -i 192.168.5.131, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template dest=/home/lab_git_repo" -u root -k

@prakashjawari
Copy link

ansible all -i 192.168.56.103, -m group -a "name=deploy"
ansible all -i 192.168.56.103, -m user -a "name=deploy-user group=deploy shell=/bin/bash state=present"
ansible all -i 192.168.56.103, -m lineinfile -a "state=present dest=/etc/sudoers line='%wheel ALL=(ALL) NOPASSWD: ALL'"
ansible all -i 192.168.56.103, -m lineinfile -a "state=present dest=/home/deploy-user/.bash_profile line='https_proxy=web-proxy.ind.hp.com:8080'"
ansible all -i 192.168.56.103, -m yum -a "name=httpd state=present" -u deploy-user -b
ansible all -i 192.168.56.103, -m yum -a "name=wget state=present" -u deploy-user -b
ansible all -i 192.168.56.103, -m file -a "dest=/opt/index.html mode=755 state=touch" -u deploy-user -b
ansible all -i 192.168.56.103, -m lineinfile -a "state=present dest=/opt/index.html line='

Welcome to HPE ansible class on remote node

'" -u deploy-user -b
ansible all -i 192.168.56.103, -m copy -a "src=/opt/index.html dest=/var/www/html" -u deploy-user -b
ansible all -i 192.168.56.103, -m service -a "name=httpd state=started" -u deploy-user -b

@pitchela
Copy link

pitchela commented Jul 1, 2019

#ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=666 state=touch owner=root"
#ansible 127.0.0.1 -m lineinfile -a "path=/opt/index.html line='Welcome to HPE Ansible class July- Swetha'"

#ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html"

#ansible 127.0.0.1 -m service -a "name=httpd state=started"

View this in browser: http://10.76.143.12/

Output:

Welcome to HP Asible class Welcome to HPE Ansible class July- Swetha

Exercise 2 Answers

1)Write a Ansible Adhoc Commands to create a group called “deploy”
[root@localhost ~]# ansible 127.0.0.1 -m group -a "name=deploy"
127.0.0.1 | CHANGED => {
"changed": true,
"gid": 1000,
"name": "deploy",
"state": "present",
"system": false
}

  1. Write a Ansible Adhoc Commands to create a user called “deploy-user”
    which is part of group called “deploy” and with /bin/bash shell.
    [root@localhost ~]# ansible 127.0.0.1 -m user -a "name=deploy-user groups=deploy shell=/bin/bash"
    127.0.0.1 | CHANGED => {
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "groups": "deploy",
    "home": "/home/deploy-user",
    "name": "deploy-user",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
    }

[root@localhost html]# id deploy-user
uid=1000(deploy-user) gid=1001(deploy-user) groups=1001(deploy-user),1000(deploy)

  1. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.

ansible 127.0.0.1 -m yum -a "name=httpd state=latest"

root@localhost ~]# ansible 127.0.0.1 -m yum -a "name=httpd state=latest"

127.0.0.1 | SUCCESS => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": false,
"changes": {
"installed": [],
"updated": []
},
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date",
""
]
}

  1. Write a Ansible Adhoc commands to start and enable the service named “httpd”

ansible 127.0.0.1 -m service -a "name=httpd state=started"

sucessful output received.

  1. Write a Ansible commands to create a file called “index.html” in
    /var/www/html with some dummy html contents.

#ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=666 state=touch owner=root"
#ansible 127.0.0.1 -m lineinfile -a "path=/opt/index.html line='Welcome to HPE Ansible class July- Swetha'"
#ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html"

  1. Write a Ansible commands to reboot a self machine.

ansible 127.0.0.1 -a "/sbin/shutdown -r"

  1. Write a Ansible commands to install a package called “git”, “wget”.
    [root@localhost ~]# ansible 127.0.0.1 -m yum -a "name=wget state=present"
    127.0.0.1 | SUCCESS => {
    "ansible_facts": {
    "pkg_mgr": "yum"
    },
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
    "wget-1.14-18.el7_6.1.x86_64 providing wget is already installed"
    ]
    }

[root@localhost ~]# ansible 127.0.0.1 -m yum -a "name=git state=present"
127.0.0.1 | SUCCESS => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"git-1.8.3.1-20.el7.x86_64 providing git is already installed"
]
}

  1. Write a Ansible Adhoc commands to clone git repo.
    thttps://github.com/scmgalaxy/ansible-role-template
    [root@localhost ~]# ansible 127.0.0.1 -m git -a "clone=1 repo=https://github.com/scmgalaxy/ansible-role-template dest=/home/deploy-user/new"
    127.0.0.1 | CHANGED => {
    "after": "d57574b3918d8698c4d16c3241496a1708c01af3",
    "before": null,
    "changed": true
    }

@pitchela
Copy link

pitchela commented Jul 2, 2019

#ansible 127.0.0.1 -m file -a "dest=/opt/index.html mode=666 state=touch owner=root"
#ansible 127.0.0.1 -m lineinfile -a "path=/opt/index.html line='Welcome to HPE Ansible class July- Swetha'"

#ansible 127.0.0.1 -m copy -a "src=/opt/index.html dest=/var/www/html"

#ansible 127.0.0.1 -m service -a "name=httpd state=started"

View this in browser: http://10.76.143.12/

Output:

Welcome to HP Asible class Welcome to HPE Ansible class July- Swetha

Exercise Lab 3:

[root@localhost ~]# ansible all -i 10.76.137.151, -m user -a "name=deploy group=wheel" -u tester -k -K -b
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 10,
"home": "/home/deploy",
"name": "deploy",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}

  1. Write a Ansible Adhoc Commands to create a group called “deploy”
    ansible all -i 10.76.137.151, -m group -a "name=deploy state=present" -u tester -K -b -k

[root@localhost ~]# ansible all -i 10.76.137.151, -m group -a "name=deploy state=present" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1001,
"name": "deploy",
"state": "present",
"system": false
}

  1. Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “deploy” and
    with /bin/bash shell.

#ansible all -i 10.76.137.151, -m user -a "name=deploy group=deploy" -u tester -K -b -k

[root@localhost ~]# ansible all -i 10.76.137.151, -m user -a "name=deploy-user group=deploy" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1001,
"home": "/home/deploy",
"move_home": false,
"name": "deploy",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}

[root@localhost ~]# ansible all -i 10.76.137.151, -m user -a "name=deploy-user group=deploy" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/deploy-user",
"name": "deploy-user",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1002
}

On ARS:

[root@localhost ~]# id deploy
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy)

[root@localhost ~]# id deploy-user
uid=1002(deploy-user) gid=1001(deploy) groups=1001(deploy)

  1. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.
    ansible all -i 10.76.137.151, -m yum -a "name=httpd state=present" -u tester -K -b -k

Add this to /etc/yum.conf
proxy=http://web-proxy.in.hpecorp.net:8080
Otherwise below error occurs

[root@localhost ~]# ansible all -i 10.76.137.151, -m yum -a "name=httpd state=present" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
tester1
10.76.137.151 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Failure talking to yum: Cannot find a valid baseurl for repo: base/7/x86_64"
}

Before the command output on ARS is as:
[root@localhost ~]# rpm -qa | grep -i http
[root@localhost ~]#

[root@localhost ~]# ansible all -i 10.76.137.151, -m yum -a "name=httpd state=present" -u tester -K -b -k -k
SSH password:
BECOME password[defaults to SSH password]:

10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: centos-hn. viettelidc.com.vn\n * extras: centos.excellmedia.net\n * updates: centos.excellmedia.net\nResolving Dep endencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be insta lled\n--> Processing Dependency: httpd-tools = 2.4.6-89.el7.centos for package: httpd-2.4.6-89.el7.cent os.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-89.el7.centos.x86_64\n-- > Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-89.el7.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-89.el7.centos.x86_64\n--> Runnin g transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util .x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-89.el7.centos will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolu tion\n\nDependencies Resolved\n\n====================================================================== ==========\n Package Arch Version Repository Size\n========== ======================================================================\nInstalling:\n httpd x86_64 2.4.6-89.el7.centos updates 2.7 M\nInstalling for dependencies:\n apr x86_64 1.4.8-3.el7_4.1 base 103 k\n apr-util x86_64 1.5.2-6.el7 base 92 k\n httpd-tools x86_64 2.4.6-89.el7.centos updates 90 k\n mailcap noarch 2.1.41-2.el7 base 3 1 k\n\nTransaction Summary\n=========================================================================== =====\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\n Downloading packages:\n-------------------------------------------------------------------------------- \nTotal 951 kB/s | 3.0 MB 00:03 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1 .4.8-3.el7_4.1.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_6 4 2/5 \n Installing : httpd-tools-2.4.6-89.el7.centos.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-89.el7.centos.x86_64 5/5 \n Verifying : httpd-t ools-2.4.6-89.el7.centos.x86_64 1/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 2/5 \n Verifying : httpd-2.4.6-89.el7.centos.x86_64 3/5 \n Verifying : apr-1.4.8-3.el7_4.1.x86_64 4/5 \n V erifying : apr-util-1.5.2-6.el7.x86_64 5/5 \n\nInstalled:\n httpd.x8 6_64 0:2.4.6-89.el7.centos \n\nDependency Installed:\n apr. x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4. 6-89.el7.centos mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}

After the command output on ARS is as:
[root@localhost ~]# rpm -qa | grep -i http
httpd-tools-2.4.6-89.el7.centos.x86_64
httpd-2.4.6-89.el7.centos.x86_64

  1. Write a Ansible Adhoc commands to start and enable the service named “httpd”
    #ansible all -i 10.76.137.151, -m service -a "name=httpd state=started" -u tester -K -b -k
    successful message
  2. Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    ansible all -i 10.76.137.151, -m file -a "path=/var/www/html state=directory" -u tester -K -b -k
    ansible all -i 10.76.137.151, -m file -a "dest=/var/www/html/index.html mode=600 state=touch" -u tester -K -b -k
    ansible all -i 10.76.137.151, -m lineinfile -a "dest=/var/www/html/index.html line=' # Welcome to HP Ansible class '" -u tester -K -b -k

[root@localhost ~]# ansible all -i 10.76.137.151, -m file -a "path=/var/www/html state=directory" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/var/www/html",
"secontext": "system_u:object_r:httpd_sys_content_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}

[root@localhost ~]# ansible all -i 10.76.137.151, -m file -a "dest=/var/www/html/index.html mode=600 state=touch" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/var/www/html/index.html",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "root",
"secontext": "unconfined_u:object_r:httpd_sys_content_t:s0",
"size": 0,
"state": "file",
"uid": 0
}

[root@localhost ~]#
[root@localhost ~]# ansible all -i 10.76.137.151, -m lineinfile -a "dest=/var/www/html/index.html line='

Welcome to HP Ansible class

'" -u tester -K -b -k
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost html]# cat index.html

Welcome to HP Ansible class

[root@localhost html]# [root@localhost html]# [root@localhost html]# pwd /var/www/html

  1. Write a Ansible commands to reboot a self machine.
    ansible all -i 10.76.137.151, -m reboot -u tester -K -b -k

Answer: it worked and rebooted the remote host

  1. Write a Ansible commands to install a package called “git”, “wget”.
    [root@localhost ~]# ansible all -i 10.76.137.151, -m yum -a "name=git,wget state=present" -u tester -K -b -k
    SSH password:
    BECOME password[defaults to SSH password]:
    10.76.137.151 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
    "installed": [
    "git",
    "wget"
    ]
    },
    "msg": "",
    "rc": 0,
    "results": [
    "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: centos-hn.viettelidc.com.vn\n * extras: centos.excellmedia.net\n * updates: centos.excellmedia.net\nResolving Dependencies\n--> Running transaction check\n---> Package git.x86_64 0:1.8.3.1-20.el7 will be installed\n--> Processing Dependency: perl-Git = 1.8.3.1-20.el7 for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl >= 5.008 for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: rsync for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(warnings) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(vars) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(strict) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(lib) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(Git) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(Getopt::Long) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(File::stat) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(File::Temp) for package: git-1.8.3.1-20.el7.x86_64\n--> Processing Dependency: perl(File::Spec) for package: git-1.8.3.1-20.el7.x86_64\n--
  2. Create a temp directory to host the repo
    ansible all -i 10.76.137.151, -m file -a "path=/tmp/test state=directory" -u tester -k -b -K

Before
[root@localhost html]# ls -lrt /tmp | grep test
[root@localhost html]# pwd
/var/www/html

[root@localhost ~]# ansible all -i 10.76.137.151, -m file -a "path=/tmp/test state=directory" -u tester -k -b -K
SSH password:
BECOME password[defaults to SSH password]:
10.76.137.151 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}

[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ls -lrt /tmp | grep test
drwxr-xr-x. 2 root root 6 Jul 1 12:30 test

  1. Write a Ansible Adhoc commands to clone git repo. T https://github.com/scmgalaxy/ansible-role-template
    #ansible all -i 10.76.137.151, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test" -u tester -k -K -b

[root@localhost ~]# ansible all -i 10.76.137.151, -m git -a "repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test" -u tester -k -K -b
SSH password:
BECOME password[defaults to SSH password]:
tester1

10.76.137.151 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"cmd": "/bin/git clone --origin origin https://github.com/scmgalaxy/ansible-role-template /tmp/test",
"msg": "fatal: unable to access 'https://github.com/scmgalaxy/ansible-role-template/': Failed connect to github.com:443; Connection timed out",
"rc": 128,
"stderr": "fatal: unable to access 'https://github.com/scmgalaxy/ansible-role-template/': Failed connect to github.com:443; Connection timed out\n",
"stderr_lines": [
"fatal: unable to access 'https://github.com/scmgalaxy/ansible-role-template/': Failed connect to github.com:443; Connection timed out"
],
"stdout": "Cloning into '/tmp/test'...\n",
"stdout_lines": [
"Cloning into '/tmp/test'..."
]
}

This is because of lack of connectivity due to proxy settings inhpe n/w
To try at home n/w..

@nandiniabbinaholal
Copy link

playbook exercise:

  • name: create group and user
    hosts: localhost
    tasks:

    • name: create group
      group: name=deploy1 state=present
    • name: create user
      user: name=deploy-user1 group=deploy1 state=present
  • name: This sets up git
    hosts: localhost
    tasks:

    • name: intall git
      yum: name=git state=present
    • name: install wget
      yum: name=wget state=present
    • name: clone git repo
      git: repo=https://github.com/scmgalaxy/ansible-role-template dest=/src/ansible-examples separate_git_dir=/src/ansible-examples.gitstate=present
  • name: This sets up an httpd webserver
    hosts: localhost
    tasks:

    • name: Install the httpd apps
      yum: name=httpd
    • name: start the httpd service
      service: name=httpd state=started
  • name: create file and copy
    hosts: localhost
    tasks:

    • name: create file
      file: dest=/opt/index.html mode=600 state=touch
    • name: copy contents
      copy: dest=/opt/index.html content='

      Welcome to HP Ansible class

      '
    • name: copy file
      copy: src=/opt/index.html dest=/var/www/html/index.html

@prakashjawari
Copy link


  • name: Create a group
    hosts: web
    tasks:

    • name: Create a group for test-deploy
      group: name=test-deploy
  • name: Creating a User for group test-deploy
    hosts: web
    tasks:

    • name: Creating a user test for test-deploy group
      user: name=test group=test-deploy shell=/bin/bash state=present
  • name: Creating a file index.xml from user test
    hosts: web
    tasks:

    • name: Creating a index.html for testing a web service
      file: dest=/opt/index.html mode=755 state=touch
  • name: Updating a index.html
    hosts: web
    tasks:

    • name: Updating a index.htm
      lineinfile: state=present dest=/opt/index.html line='

      Welcome to HP Ansible class

      '
  • name: Copy Index file to the /var/www/html
    hosts: web
    tasks:

    • name: Copy index file to the http directory
      copy: src=/opt/index.html dest=/var/www/html
  • name: Installing git and wget
    hosts: web
    tasks:

    • name: Installing wget and git
      yum: name=wget state=present name=git state=present
  • name: Start http service on multiple machine
    hosts: web
    tasks:

    • name: starting a web
      service: name=httpd state=started
      ...

@neetesh80
Copy link

Lab4 - Playbooks


- import_playbook: user.yaml
- import_playbook: web.yaml
- import_playbook: web_contents.yaml
#- import_playbook: reboot.yaml
- import_playbook: pkg.yaml
- import_playbook: repo.yaml
-  name: Create a user and add it to the group
   hosts: web
   tasks:
     - name: Create a group
       group: name=deploy state=present
     - name: Create a user and add to group
       user: name=deploy group=deploy
-  name: Setup a web server
   hosts: web
   tasks:
     - name: Install Apache
       yum: name=httpd
     - name: Start Apache server
       service: name=httpd state=started
-  name: Write contents to a file on webserver
   hosts: web
   tasks:
     - name: Create a directory
       file: path=/var/www/html state=directory
     - name: Create a file
       file: dest=/var/www/html/index.html mode=600 state=touch
     - name: Write contents to the file
       lineinfile: dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible class </h1>  </html>'
-  name: Reboot the Apache server
   hosts: web
   tasks:
     - name: Restart the httpd server
       reboot:
---
-  name: Install git and wget packages
   hosts: web
   tasks:
     - name: Install git, wget packages
       yum: name=git,wget state=present
-  name: Clone the repo
   hosts: web
   tasks:
     - name: Create a temp directory to host repo
       file: path=/tmp/test state=directory
     - name: Clone the repo using url
       git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment