cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Etc


Reference














.

middleware:iac:ansible:index.html



Middleware

Ansible CLI Commands Cheet Sheat and Configuration Examples

Web Sites

Installation

RHEL/CentOS

CentOS7

yum install ansible

CentOS6

yum install epel-release
yum install ansible


Preliminary work

Target Server

useradd ansible
echo 'ansible:ansible' | chpasswd     <-  change password

cp -pi /etc/sudoers{,.`date '+%Y%m%d'`}
echo "ansible ALL=(ALL)       NOPASSWD:ALL" >> /etc/sudoers

Ansible Server

useradd ansible
echo 'ansible:ansible' | chpasswd     <-  change password

su - ansible
ssh-keygen -t rsa
ssh-copy-id   x.x.x.x    <- Copy Publick Key to Target Server


ansible Command Example

[ansible@centos7 ~]$ ansible all -i 192.168.0.37, -a "hostname" -k
SSH password:
192.168.0.37 | SUCCESS | rc=0 >>
centos7b


How to use hosts file

cd /home/ansible
mkdir ansible1   <- ansible working directory
cd ansible1

touch hosts
vi hosts
-----------------------
[test]
192.168.0.37
-----------------------

[ansible@centos7 ansible1]$ ansible test -i hosts -a "hostname" -k
SSH password:
192.168.0.37 | SUCCESS | rc=0 >>
centos7b

[ansible@centos7 ansible1]$


How to use hosts file with password

[ansible@centos7 ansible1]$cat hosts
----------------------------
[all:vars]
ansible_ssh_pass=ansible

[test]
192.168.0.37
----------------------------

[ansible@centos7 ansible1]$ ansible test -i hosts -a "hostname"
192.168.0.37 | SUCCESS | rc=0 >>
centos7b

[ansible@centos7 ansible1]$

[ansible@centos7 ansible1]$ cat hosts
[test]
192.168.0.37 ansible_ssh_pass=ansible

[ansible@centos7 ansible1]$
[ansible@centos7 ansible1]$ ansible test -i hosts -a "hostname"
192.168.0.37 | SUCCESS | rc=0 >>
centos7b

[ansible@centos7 ansible1]$


ansible command sample

$ ansible testservers -a "/bin/date"
$ ansible webservers -a "/sbin/reboot" -f 10 --sudo

-m ping

$ ansible all -m ping
$ ansible -i /tmp/hosts all -m ping

-m command

$ ansible group1 -i /tmp/hosts all -m command -a "touch /tmp/test1.txt"

-m shell

$ ansible testserver -m shell -a 'echo $TERM'

-m copy

$ ansible testservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"

-m file

$ ansible testservers -m file -a "dest=/tmp/hosts mode=600 owner=root group=root" -s -K

-m yum

$ ansible webservers -m yum -a "name=httpd state=installed"


man ansible

$ man ansible

NAME
       ansible - run a task on a target host(s)

SYNOPSIS
       ansible <host-pattern> [-m module_name] [-a args] [options]

ARGUMENTS
       host-pattern
           A name of a group in the inventory, a shell-like glob selecting hosts in inventory or any combination
           of the two separated by commas.

OPTIONS
       -i PATH, --inventory=PATH
           The PATH to the inventory, which defaults to /etc/ansible/hosts. Alternatively you can use a comma
           separated list of hosts or single host with traling comma host,.

       -a 'ARGUMENTS', --args='ARGUMENTS'
           The ARGUMENTS to pass to the module.

       -m NAME, --module-name=NAME
           Execute the module called NAME.

       -s, --sudo
           Run the command as the user given by -u and sudo to root (deprecated, use become).

       K, --ask-become-pass
           Ask for privilege escalation password.








middleware/iac/ansible/index.html.txt ยท Last modified: 2020/04/12 by admin

Page Tools