Pages

Thursday, January 21, 2016

Ansible – More Modules and Commands


In this article we will see a more in-depth uses of the Ansible command using the Same hosts file.

Ping the Local Machine
[root@vx111a docker]# ansible all -i 'localhost,' -c local -m ping
localhost | success >> {
    "changed": false,
    "ping": "pong"
}

List all the Hosts Defined in the Inventory File
[root@vx111a docker]# ansible -i hosts all --list-hosts
    172.16.202.96

Execute a Command on the Remote Machine defined in the Hosts File
[root@vx111a docker]# ansible -i hosts all  -m shell -a "uptime" --user vagrant
172.16.202.96 | success | rc=0 >>
 15:41:20 up 4 days,  5:10,  1 user,  load average: 0.00, 0.00, 0.00

Execute the Command on the Local machine
[root@vx111a docker]# ansible all -i 'localhost,' -c local -m shell -a "uptime"
localhost | success | rc=0 >>
 20:21:00 up 13 days, 15 min,  4 users,  load average: 0.22, 0.14, 0.14

Copy a File
[root@vx111a docker]# ansible -i hosts all -m copy -a "src=/work/docker/Dockerfile dest=/tmp" --user vagrant
172.16.202.96 | success >> {
    "changed": true,
    "checksum": "591af5f15567c89b7ebcaf4e7d9ca4657ea63135",
    "dest": "/tmp/Dockerfile",
    "gid": 500,
    "group": "vagrant",
    "md5sum": "2ec05713f50f4c2026aa514329111722",
    "mode": "0664",
    "owner": "vagrant",
    "size": 132,
    "src": "/home/vagrant/.ansible/tmp/ansible-tmp-1453200901.47-275728752624352/source",
    "state": "file",
    "uid": 500
}


Set File Permissions
[root@vx111a docker]# ansible -i hosts all -m file -a "dest=/tmp/Dockerfile mode=664" --user vagrant
172.16.202.96 | success >> {
    "changed": false,
    "gid": 500,
    "group": "vagrant",
    "mode": "0664",
    "owner": "vagrant",
    "path": "/tmp/Dockerfile",
    "size": 132,
    "state": "file",
    "uid": 500
}

Create a Directory
[root@vx111a docker]# ansible -i hosts all -m file -a "dest=/tmp/pump mode=755 state=directory" --user vagrant
172.16.202.96 | success >> {
    "changed": true,
    "gid": 500,
    "group": "vagrant",
    "mode": "0755",
    "owner": "vagrant",
    "path": "/tmp/pump",
    "size": 4096,
    "state": "directory",
    "uid": 500
}

Delete a Directory
[root@vx111a docker]# ansible -i hosts all -m file -a "dest=/tmp/pump state=absent" --user vagrant
172.16.202.96 | success >> {
    "changed": true,
    "path": "/tmp/pump",
    "state": "absent"
}

Get Package Status
[root@vx111a docker]# ansible -i hosts all -m yum -a "name=wget state=present" --user vagrant
172.16.202.96 | success >> {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "wget-1.12-1.4.el6.x86_64 providing wget is already installed"
    ]
}

Get Service Status
[root@vx111a docker]# ansible -i hosts all -m service -a "name=auditd state=started" --user vagrant
172.16.202.96 | success >> {
    "changed": false,
    "name": "auditd",
    "state": "started"
}

Setup – One of the very use-full commands that we use mostly is to get the details of the machine. Setup is one such module which will allow to get all details like env variables , memory configurations etc on a remote machine. In order to get these details on the local machine we can use

[root@vx111a docker]# ansible all -i 'localhost,' -c local -m setup

More to Come, Happy Learning

No comments :

Post a Comment