$ brew install ansible $ ansible --version ansible 1.8.4 configured module search path = None
hosts
: [test] localhost ansible_connection=local
$ ansible -i hosts -m ping all localhost | success >> { "changed": false, "ping": "pong" }
all
parameter) from the hosts file, execute the ping module. Let's see something else. $ ansible -i hosts -a "ls -lah" all localhost | success | rc=0 >> total 12K drwxr-xr-x 5 brun staff 170 Apr 1 11:50 . drwxr-xr-x 91 brun staff 3.1K Apr 1 11:37 .. -rw-r--r-- 1 brun staff 230 Apr 1 12:07 export.sh -rw-r--r-- 1 brun staff 42 Apr 3 14:48 hosts -rw-r--r-- 1 brun staff 376 Apr 1 12:49 playbook.yml
-m
key) is not specified, the command module is used. In fact, ansible can be used not only as a configuration management system, but also as a framework for distributed command execution. # hosts [web] 111.111.111.111
ansible.cfg
, in the project directory. # ansible.cfg [defaults] hostfile = hosts
web.yml
. # web.yml --- - hosts: all user: ubuntu tasks: - name: Update apt cache apt: update_cache=yes sudo: yes - name: Install required packages apt: name={{ item }} sudo: yes with_items: - nginx - postgresql
$ ansible-playbook web.yml PLAY [all] ******************************************************************** GATHERING FACTS *************************************************************** ok: [111.111.111.111] TASK: [Update apt cache] ****************************************************** ok: [111.111.111.111] TASK: [Install required packages] ********************************************* changed: [111.111.111.111] => (item=nginx,postgresql) PLAY RECAP ******************************************************************** 111.111.111.111 : ok=3 changed=1 unreachable=0 failed=0
$ ansible-galaxy init nginx -p roles - nginx was created successfully $ tree βββ roles β βββ nginx β βββ README.md β βββ defaults β β βββ main.yml β βββ files β βββ handlers β β βββ main.yml β βββ meta β β βββ main.yml β βββ tasks β β βββ main.yml β βββ templates β βββ vars β βββ main.yml
web.yml
finish the web.yml
file. --- - hosts: all user: ubuntu sudo: yes roles: - nginx
sudo: yes
key. He allows not to write this line in each task. Most administrative tasks should still run as root
, so it makes sense to leave it here.roles/nginx/tasks/main.yml
we write the following: --- - name: Update apt cache apt: update_cache=yes - name: Install required packages apt: name=nginx
setup
module that is run first for all managed nodes. Let's see what he does. $ ansible -m setup all -u ubuntu "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.31.7.80" ], "ansible_architecture": "x86_64", "ansible_bios_date": "12/03/2014", "ansible_bios_version": "4.2.amazon", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-3.13.0-44-generic", "console": "ttyS0", "ro": true, "root": "UUID=fd803688-5c41-4188-8a06-382a65a520bf" }, "ansible_default_ipv4": { "address": "172.31.7.80", "alias": "eth0", "gateway": "172.31.0.1", "interface": "eth0", "macaddress": "06:a8:07:41:47:a5", "mtu": 9001, "netmask": "255.255.240.0", "network": "172.31.0.0", "type": "ether" } ...
ansible_default_ipv4
. # web.yml tasks: - debug: msg={{ansible_default_ipv4}}
$ ansible-playbook web.yml PLAY [all] ******************************************************************** GATHERING FACTS *************************************************************** ok: [111.111.111.111] TASK: [debug msg="{{ansible_default_ipv4}}"] ********************************** ok: [111.111.111.111] => { "msg": "{u'macaddress': u'06:a8:07:41:47:a5', u'network': u'172.31.0.0', u'mtu': 9001, u'alias': u'eth0', u'netmask': u'255.255.240.0', u'address': u'172.31.7.80', u'interface': u'eth0', u'type': u'ether', u'gateway': u'172.31.0.1'}" }
# roles/nginx/templates/ansible.conf.j2 server { listen 80 default_server; root /usr/share/nginx/html; index index.html index.htm; }
# roles/nginx/templates/index.html.j2 <html> <body> <pre> {{ ansible_default_ipv4 }} {{ ansible_env }} </pre> </body> </html>
{{ ansible_env }}
) are the ansible variables that we collected using the setup
module we already know. # roles/nginx/handlers/main.yml --- # handlers file for nginx - name: reload nginx service: name=nginx state=reloaded
# roles/nginx/tasks/main.yml --- - name: Update apt cache apt: update_cache=yes - name: Install required packages apt: name=nginx - name: Start nginx service service: name=nginx state=started - name: Delete default nginx site file: path=/etc/nginx/sites-enabled/default state=absent notify: reload nginx - name: Create default nginx site template: src=ansible.conf.j2 dest=/etc/nginx/sites-enabled/ansible owner=www-data group=www-data notify: reload nginx - name: Create index.html file template: src=index.html.j2 dest=/usr/share/nginx/html/index.html owner=www-data group=www-data
ansible.conf.j2
, which we wrote above, and generates the index.html
file from the template that we described above . Please note that some tasks send notify: reload nginx
notifications notify: reload nginx
. Moreover, in this case, 2 notifications should be sent to reboot nginx, but, in fact, they will be merged into one, as will be seen below. Notifications are needed to reboot nginx (or any other service) only in case the template (or something else) has changed, so as not to do it every time you start ansible. $ ansible-playbook web.yml PLAY [all] ******************************************************************** GATHERING FACTS *************************************************************** ok: [111.111.111.111] TASK: [nginx | Update apt cache] ********************************************** ok: [111.111.111.111] TASK: [nginx | Install required packages] ************************************* ok: [111.111.111.111] TASK: [nginx | Start nginx service] ******************************************* ok: [111.111.111.111] TASK: [nginx | Delete default nginx site] ************************************* changed: [111.111.111.111] TASK: [nginx | Create default nginx site] ************************************* changed: [111.111.111.111] TASK: [nginx | Create index.html file] **************************************** changed: [111.111.111.111] TASK: [debug msg="{{ansible_default_ipv4}}"] ********************************** ok: [111.111.111.111] => { "msg": "{u'macaddress': u'06:a8:07:41:47:a5', u'network': u'172.31.0.0', u'mtu': 9001, u'alias': u'eth0', u'netmask': u'255.255.240.0', u'address': u'172.31.7.80', u'interface': u'eth0', u'type': u'ether', u'gateway': u'172.31.0.1'}" } NOTIFIED: [nginx | reload nginx] ********************************************** changed: [111.111.111.111] PLAY RECAP ******************************************************************** 111.111.111.111 : ok=9 changed=4 unreachable=0 failed=0
-vvvv
key, -vvvv
not always clear which command was executed on the server and what prevented it from executing.Source: https://habr.com/ru/post/254959/
All Articles