Ansible
Jump to navigation
Jump to search
- There is a master machine (where the command is dispatched, where ansible is installed and will orchestrate the commands) and a set of workers machines (where the commands must ran)
Install
Install ansible in the master machine
- From Debian_backports
su -c "apt-get -t stretch-backports install ansible"
OR
- From Ubuntu repository (old, failsafe)
su - apt-get install dirmngr apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367 add-apt-repository "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" etckeeper commit apt-get update apt-get install ansible # test ansible --version exit
Config
- The follow is a simple config that configure a master and a worker machine. Note that:
- sshd must be installed and running in each worker machine
- ansible must be installed in the master machine, using the commands above
- configure ansible /etc/ansible/hosts file, specifying the hostname and ports of each worker (in this example we will configure 2 workers that are the same local machine, 'localhost' and '127.0.0.1', using default ssh port '22')
# the follow line just add 'localhost:22' line at the end of the config file su - echo 'localhost:22' >> /etc/ansible/hosts echo '127.0.0.1:22' >> /etc/ansible/hosts exit
- enable the ssh access to each worker, adding the public key of the user that will start the command
# in this case, the local pub key is added in the same machine user list of authorized keys cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys # in a multi-machine real environment, the public key must be added in the host as well as in each worker
Run
- From the host machine, the user that has the public key configured can run a set of commands defined in a playbook file, this is an example of such file, filename.yaml:
--- - hosts: all become: False tasks: - name: show the hostname shell: hostname - name: show the current folder contents shell: ls
The file specify a list item (each list item starts with a '-') to configure the workers where run (in this case, all hosts in /etc/ansible/hosts), does not requires root privileges defining 'become: False', and finally define the tasks as a list with two items, one that run the command 'hostname' and one that run the 'ls' command
- The previous file can be submitted with the follow command:
ansible-playbook -v filename.yaml
- The output is like the follows:
Using /etc/ansible/ansible.cfg as config file [WARNING]: A duplicate localhost-like entry was found (127.0.0.1). First found localhost was localhost PLAY [all] *************************************************************************************** TASK [Gathering Facts] *************************************************************************** ok: [127.0.0.1] ok: [localhost] TASK [show the hostname] ************************************************************************* changed: [127.0.0.1] => {"changed": true, "cmd": "hostname", "delta": "0:00:00.001861", "end": "2018-05-18 17:10:06.269964", "rc": 0, "start": "2018-05-18 17:10:06.268103", "stderr": "", "stderr_lines": [], "stdout": "YOURHOSTNAME", "stdout_lines": ["YOURHOSTNAME"]} changed: [localhost] => {"changed": true, "cmd": "hostname", "delta": "0:00:00.001636", "end": "2018-05-18 17:10:06.270706", "rc": 0, "start": "2018-05-18 17:10:06.269070", "stderr": "", "stderr_lines": [], "stdout": "YOURHOSTNAME", "stdout_lines": ["YOURHOSTNAME"]} TASK [show the current folder contents] ********************************************************** changed: [localhost] => {"changed": true, "cmd": "ls", "delta": "0:00:00.002065", "end": "2018-05-18 17:10:06.466266", "rc": 0, "start": "2018-05-18 17:10:06.464201", "stderr": "", "stderr_lines": [], "stdout": "FILE_1\nFILE_2\nFILE_...\nFILE_N", "stdout_lines": ["FILE_1", "FILE_2", "FILE_...", "FILE_N"]} changed: [127.0.0.1] => {"changed": true, "cmd": "ls", "delta": "0:00:00.001849", "end": "2018-05-18 17:10:06.476125", "rc": 0, "start": "2018-05-18 17:10:06.474276", "stderr": "", "stderr_lines": [], "stdout": "FILE_1\nFILE_2\nFILE_...\nFILE_N", "stdout_lines": ["FILE_1", "FILE_2", "FILE_...", "FILE_N"]} PLAY RECAP *************************************************************************************** 127.0.0.1 : ok=3 changed=2 unreachable=0 failed=0 localhost : ok=3 changed=2 unreachable=0 failed=0