├── .gitignore ├── Chapter01 ├── example01 │ └── README.md ├── example02 │ └── mastery-hosts ├── example03 │ └── mastery-hosts ├── example04 │ └── README.md ├── example05 │ ├── README.md │ └── snippet.yaml ├── example06 │ ├── README.md │ ├── mastery-hosts │ └── mastery.yaml ├── example07 │ ├── README.md │ ├── mastery-hosts │ └── mastery.yaml ├── example08 │ ├── README.md │ ├── mastery-hosts │ ├── prepost.yaml │ └── roles │ │ └── simple │ │ ├── defaults │ │ └── main.yaml │ │ └── tasks │ │ └── main.yaml ├── example09 │ ├── README.md │ ├── a_vars_file.yaml │ ├── mastery-hosts │ ├── relative.yaml │ └── tasks │ │ ├── a.yaml │ │ └── b.yaml ├── example10 │ ├── README.md │ ├── mastery-hosts │ └── names.yaml ├── example11 │ └── README.md ├── example12 │ ├── README.md │ └── snippet.yaml ├── example13 │ ├── README.md │ └── plugin_filters.yml ├── example14 │ ├── README.md │ ├── priority-hosts │ └── priorityordering.yaml └── example15 │ ├── README.md │ ├── priority-hosts │ └── priorityordering.yaml ├── Chapter02 ├── example01 │ └── README.md ├── example02 │ ├── README.md │ └── password_file ├── example03 │ ├── README.md │ ├── password.sh │ └── password_file ├── example04 │ ├── README.md │ ├── a_vars_file.yaml │ └── password.sh ├── example05 │ ├── README.md │ ├── a_vars_file.yaml │ ├── mastery-hosts │ ├── password.sh │ └── showme.yaml ├── example06 │ ├── README.md │ ├── inline.yaml │ ├── mastery-hosts │ └── password.sh └── example07 │ ├── README.md │ ├── a_vars_file.yaml │ ├── mastery-hosts │ ├── password.sh │ └── showme.yaml ├── Chapter03 ├── example01 │ └── README.md ├── example02 │ └── README.md ├── example03 │ ├── README.md │ └── windows-hosts ├── example04 │ ├── README.md │ └── windows-hosts ├── example05 │ └── README.md ├── example06 │ ├── README.md │ └── windows-hosts └── example07 │ ├── README.md │ ├── mastery.txt │ ├── win_file.yaml │ └── windows-hosts ├── Chapter04 ├── example01 │ ├── README.md │ └── awx.conf └── example02 │ ├── README.md │ └── example.yaml ├── Chapter05 ├── example01 │ ├── README.md │ ├── demo.j2 │ ├── mastery-hosts │ └── template-demo.yaml ├── example02 │ ├── README.md │ ├── demo.j2 │ ├── mastery-hosts │ └── template-demo.yaml ├── example03 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-v2.yaml ├── example04 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-v2.yaml ├── example05 │ ├── README.md │ ├── demo-for.j2 │ ├── mastery-hosts │ └── template-demo-for.yaml ├── example06 │ ├── README.md │ ├── demo-for.j2 │ ├── mastery-hosts │ └── template-demo-for.yaml ├── example07 │ ├── README.md │ ├── demo-for.j2 │ ├── mastery-hosts │ └── template-demo-for.yaml ├── example08 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example09 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example10 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example11 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example12 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example13 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example14 │ ├── README.md │ ├── demo-macro.j2 │ ├── mastery-hosts │ └── template-demo-macro.yaml ├── example15 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example16 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example17 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example18 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example19 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example20 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example21 │ ├── README.md │ ├── derp │ ├── mastery-hosts │ └── template-demo-filters.yaml ├── example22 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-objects.yaml └── example23 │ ├── README.md │ ├── mastery-hosts │ └── template-demo-comparisons.yaml ├── Chapter06 ├── example01 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example02 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example03 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example04 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example05 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example06 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example07 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example08 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example09 │ ├── README.md │ ├── error.yaml │ ├── files │ │ └── frobitz │ └── mastery-hosts ├── example10 │ ├── README.md │ ├── error.yaml │ ├── files │ │ └── frobitz │ └── mastery-hosts ├── example11 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example12 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example13 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example14 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example15 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example16 │ ├── README.md │ ├── error.yaml │ └── mastery-hosts ├── example17 │ ├── README.md │ ├── loop.yaml │ └── mastery-hosts └── example18 │ ├── README.md │ ├── loop.yaml │ └── mastery-hosts ├── Chapter07 ├── example01 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── more-tasks.yaml ├── example02 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── more-tasks.yaml ├── example03 │ ├── README.md │ ├── files.yaml │ ├── includer.yaml │ └── mastery-hosts ├── example04 │ ├── README.md │ ├── files.yaml │ ├── includer.yaml │ └── mastery-hosts ├── example05 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── more-tasks.yaml ├── example06 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── more-tasks.yaml ├── example07 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── more-tasks.yaml ├── example08 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── more-tasks.yaml ├── example09 │ ├── README.md │ ├── handlers.yaml │ ├── includer.yaml │ └── mastery-hosts ├── example10 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── variables.yaml ├── example11 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── variables.yaml ├── example12 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── variables.yaml ├── example13 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── variables.yaml ├── example14 │ ├── README.md │ ├── includer.yaml │ ├── mastery-hosts │ └── variables.yaml ├── example15 │ ├── README.md │ ├── includeme.yaml │ ├── includer.yaml │ └── mastery-hosts ├── example16 │ ├── README.md │ ├── mastery-hosts │ ├── roleplay.yaml │ └── roles │ │ └── simple │ │ ├── defaults │ │ └── main.yml │ │ └── tasks │ │ └── main.yml ├── example17 │ ├── README.md │ ├── mastery-hosts │ ├── roleplay.yaml │ └── roles │ │ └── simple │ │ ├── defaults │ │ └── main.yml │ │ └── tasks │ │ └── main.yml ├── example18 │ ├── README.md │ ├── mastery-hosts │ ├── roleplay.yaml │ └── roles │ │ └── simple │ │ ├── defaults │ │ └── main.yml │ │ ├── handlers │ │ └── main.yml │ │ └── tasks │ │ └── main.yml └── example19 │ └── README.md ├── Chapter08 ├── example01 │ ├── README.md │ ├── mastery-hosts │ └── vintro.yaml ├── example02 │ ├── README.md │ ├── mastery-hosts │ └── vintro.yaml ├── example03 │ ├── README.md │ ├── mastery-hosts │ └── vintro.yaml ├── example04 │ ├── README.md │ ├── mastery-hosts │ └── vintro.yaml ├── example05 │ ├── README.md │ ├── mastery-hosts │ └── objmethod.yaml ├── example06 │ ├── README.md │ ├── mastery-hosts │ └── objmethod.yaml ├── example07 │ ├── README.md │ ├── mastery-hosts │ └── objmethod.yaml ├── example08 │ ├── README.md │ ├── mastery-hosts │ └── objmethod.yaml ├── example09 │ ├── README.md │ ├── mastery-hosts │ └── objmethod.yaml └── example10 │ ├── README.md │ ├── library │ └── systemd.py │ ├── mastery-debug │ └── rpdb.yaml ├── Chapter09 ├── example01 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example02 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example03 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example04 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example05 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example06 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example07 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example08 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ └── simple_module.yaml ├── example09 │ ├── README.md │ ├── filter_plugins │ │ └── sample_filter.py │ ├── mastery-hosts │ └── simple_filter.yaml ├── example10 │ ├── README.md │ ├── ansible.cfg │ ├── callback_plugins │ │ └── shrug.py │ ├── filter_plugins │ │ └── sample_filter.py │ ├── mastery-hosts │ └── simple_filter.yaml ├── example11 │ ├── README.md │ ├── ansible.cfg │ ├── inventory_test.yaml │ └── mastery-inventory.py ├── example12 │ ├── README.md │ ├── ansible.cfg │ ├── inventory_test.yaml │ └── mastery-inventory.py └── example13 │ └── README.md ├── Chapter10 ├── example01 │ ├── README.md │ ├── library │ │ └── remote_copy.py │ ├── mastery-hosts │ ├── serial.yaml │ └── simple_module.yaml ├── example02 │ ├── README.md │ └── serial.yaml ├── example03 │ ├── README.md │ ├── failtest.yaml │ └── mastery-hosts ├── example04 │ ├── README.md │ ├── failtest.yaml │ └── mastery-hosts ├── example05 │ ├── README.md │ ├── failtest.yaml │ └── mastery-hosts ├── example06 │ ├── README.md │ ├── failtest.yaml │ └── mastery-hosts ├── example07 │ ├── README.md │ ├── failtest.yaml │ └── mastery-hosts ├── example08 │ ├── README.md │ ├── mastery-hosts │ ├── micro.yaml │ └── roles │ │ ├── microA │ │ ├── handlers │ │ │ └── main.yaml │ │ └── tasks │ │ │ └── main.yaml │ │ └── microB │ │ ├── handlers │ │ └── main.yaml │ │ └── tasks │ │ └── main.yaml ├── example09 │ ├── README.md │ ├── mastery-hosts │ └── runonce.yaml └── example10 │ ├── README.md │ ├── forserial.yaml │ └── mastery-hosts ├── Chapter11 ├── example01 │ ├── README.md │ ├── boot-server.yaml │ └── mastery-hosts ├── example02 │ ├── README.md │ ├── boot-server.yaml │ └── mastery-hosts ├── example03 │ ├── README.md │ ├── boot-server.yaml │ └── mastery-hosts ├── example04 │ ├── README.md │ ├── configure-server.yaml │ └── mastery-hosts ├── example05 │ ├── README.md │ ├── boot-ec2-server.yaml │ └── mastery-hosts ├── example06 │ ├── README.md │ ├── boot-azure-server.yaml │ └── mastery-hosts ├── example07 │ ├── Dockerfile │ └── README.md ├── example08 │ ├── Dockerfile │ ├── README.md │ ├── docker-interact.yaml │ └── mastery-hosts ├── example09 │ ├── README.md │ ├── docker-all.yaml │ └── mastery-hosts ├── example10 │ └── README.md └── example11 │ ├── README.md │ └── ansible │ ├── ansible-requirements.txt │ ├── ansible.cfg │ ├── container.yml │ ├── meta.yml │ ├── requirements.yml │ └── roles │ └── cowsay │ └── tasks │ └── main.yml ├── Chapter12 ├── example01 │ ├── README.md │ └── reset-f5.yaml ├── example02 │ ├── README.md │ ├── switch-inventory │ ├── switch-l2-configure.yaml │ └── switch-query.yaml ├── example03 │ ├── README.md │ ├── switch-facts.yaml │ └── switch-inventory └── example04 │ ├── README.md │ ├── switch-inventory │ ├── switch-l2-configure.yaml │ └── switch-query.yaml ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.pyc 2 | 3 | -------------------------------------------------------------------------------- /Chapter01/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this command: 8 | 9 | ansible-playbook --version 10 | -------------------------------------------------------------------------------- /Chapter01/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | -------------------------------------------------------------------------------- /Chapter01/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | 24 | [all:vars] 25 | ansible_ssh_user=otto 26 | -------------------------------------------------------------------------------- /Chapter01/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run these commands: 8 | 9 | curl -O https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py 10 | curl -O https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini 11 | sudo yum -y install python-boto 12 | 13 | Once you have set your AWS credentials as described in the book run: 14 | 15 | chmod +x ec2.py 16 | ./ec2.py --list 17 | 18 | If you have any virtual instances defined in EC2, run: 19 | 20 | ansible -i ec2.py all -m ping 21 | 22 | -------------------------------------------------------------------------------- /Chapter01/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | This is a playbook snippet only - it cannot be run by itself but is included for completeness. Remember to set the indentation correctly if you incorporate it into your own playbook. 8 | -------------------------------------------------------------------------------- /Chapter01/example05/snippet.yaml: -------------------------------------------------------------------------------- 1 | - name: add new node into runtime inventory 2 | add_host: 3 | name: newmastery.example.name 4 | groups: web 5 | ansible_host: 192.168.10.30 6 | -------------------------------------------------------------------------------- /Chapter01/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following commands: 8 | 9 | ansible-playbook -i mastery-hosts -c local mastery.yaml 10 | ansible-playbook -i mastery-hosts -c local --limit frontend mastery.yaml 11 | -------------------------------------------------------------------------------- /Chapter01/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | 24 | [all:vars] 25 | ansible_ssh_user=otto 26 | -------------------------------------------------------------------------------- /Chapter01/example06/mastery.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: limit example play 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: tell us which host we are on 8 | debug: 9 | var: inventory_hostname 10 | -------------------------------------------------------------------------------- /Chapter01/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following command: 8 | 9 | ansible-playbook -i mastery-hosts -c local --limit frontend mastery.yaml 10 | -------------------------------------------------------------------------------- /Chapter01/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | 24 | [all:vars] 25 | ansible_ssh_user=otto 26 | -------------------------------------------------------------------------------- /Chapter01/example07/mastery.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: limit example play 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: tell us which host we are on 8 | debug: 9 | var: inventory_hostname 10 | 11 | - name: grab variable data from backend 12 | debug: 13 | var: hostvars['backend.example.name']['ansible_port'] 14 | -------------------------------------------------------------------------------- /Chapter01/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following command: 8 | 9 | ansible-playbook -i mastery-hosts prepost.yaml 10 | -------------------------------------------------------------------------------- /Chapter01/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | 24 | [all:vars] 25 | ansible_ssh_user=otto 26 | -------------------------------------------------------------------------------- /Chapter01/example08/prepost.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | gather_facts: false 4 | 5 | vars: 6 | - a_var: derp 7 | 8 | pre_tasks: 9 | - name: pretask 10 | debug: 11 | msg: "a pre task" 12 | changed_when: true 13 | notify: say hi 14 | 15 | roles: 16 | - role: simple 17 | derp: newval 18 | 19 | tasks: 20 | - name: task 21 | debug: 22 | msg: "a task" 23 | changed_when: true 24 | notify: say hi 25 | 26 | post_tasks: 27 | - name: posttask 28 | debug: 29 | msg: "a post task" 30 | changed_when: true 31 | notify: say hi 32 | 33 | handlers: 34 | - name: say hi 35 | debug: 36 | msg: hi 37 | -------------------------------------------------------------------------------- /Chapter01/example08/roles/simple/defaults/main.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | derp: herp 3 | -------------------------------------------------------------------------------- /Chapter01/example08/roles/simple/tasks/main.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: print a variable 3 | debug: 4 | var: derp 5 | changed_when: true 6 | notify: say hi 7 | -------------------------------------------------------------------------------- /Chapter01/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following command: 8 | 9 | ansible-playbook -i mastery-hosts -c local relative.yaml 10 | -------------------------------------------------------------------------------- /Chapter01/example09/a_vars_file.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | something: "better than nothing" 3 | -------------------------------------------------------------------------------- /Chapter01/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | 24 | [all:vars] 25 | ansible_ssh_user=otto 26 | -------------------------------------------------------------------------------- /Chapter01/example09/relative.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: relative path play 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars_files: 7 | - a_vars_file.yaml 8 | 9 | tasks: 10 | - name: who am I 11 | debug: 12 | msg: "I am mastery task" 13 | 14 | - name: var from file 15 | debug: 16 | var: something 17 | 18 | - include: tasks/a.yaml 19 | -------------------------------------------------------------------------------- /Chapter01/example09/tasks/a.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: where am I 3 | debug: 4 | msg: "I am task a" 5 | 6 | - include: b.yaml 7 | 8 | -------------------------------------------------------------------------------- /Chapter01/example09/tasks/b.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: who am I 3 | debug: 4 | msg: "I am task b" 5 | -------------------------------------------------------------------------------- /Chapter01/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following command: 8 | 9 | ansible-playbook -i mastery-hosts -c local names.yaml 10 | -------------------------------------------------------------------------------- /Chapter01/example10/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | 24 | [all:vars] 25 | ansible_ssh_user=otto 26 | -------------------------------------------------------------------------------- /Chapter01/example10/names.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: play with a {{ var_name }} 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars: 7 | - var_name: not-mastery 8 | 9 | tasks: 10 | - name: set a variable 11 | set_fact: 12 | task_var_name: "defined variable" 13 | 14 | - name: task with a {{ task_var_name }} 15 | debug: 16 | msg: "I am mastery task" 17 | 18 | - name: second play with a {{ task_var_name }} 19 | hosts: localhost 20 | gather_facts: false 21 | 22 | tasks: 23 | - name: task with a {{ runtime_var_name }} 24 | debug: 25 | msg: "I am another mastery task" 26 | -------------------------------------------------------------------------------- /Chapter01/example11/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this command: 8 | 9 | ansible-doc debug | cat - 10 | -------------------------------------------------------------------------------- /Chapter01/example12/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | This is a playbook snippet only - it cannot be run by itself but is included for completeness. Remember to set the indentation correctly if you incorporate it into your own playbook. 8 | -------------------------------------------------------------------------------- /Chapter01/example12/snippet.yaml: -------------------------------------------------------------------------------- 1 | - name: add a keypair to nova 2 | os_keypair: cloud={{ cloud_name }} name=admin-key wait=yes 3 | 4 | - name: add a keypair to nova 5 | os_keypair: 6 | cloud: "{{ cloud_name }}" 7 | name: admin-key 8 | wait: yes 9 | -------------------------------------------------------------------------------- /Chapter01/example13/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | This is an example of a filter blacklist as included in the book. 8 | -------------------------------------------------------------------------------- /Chapter01/example13/plugin_filters.yml: -------------------------------------------------------------------------------- 1 | --- 2 | filter_version:'1.0' 3 | module_blacklist: 4 | # Deprecated – to be removed in 2.11 5 | - sf_account_manager 6 | -------------------------------------------------------------------------------- /Chapter01/example14/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following command: 8 | 9 | ansible-playbook -i priority-hosts -c local priorityordering.yaml 10 | -------------------------------------------------------------------------------- /Chapter01/example14/priority-hosts: -------------------------------------------------------------------------------- 1 | [frontend] 2 | host1.example.com 3 | host2.example.com 4 | 5 | [web:children] 6 | frontend 7 | 8 | [web:vars] 9 | http_port=80 10 | secure=true 11 | 12 | [proxy] 13 | host1.example.com 14 | 15 | [proxy:vars] 16 | http_port=8080 17 | thread_count=10 18 | -------------------------------------------------------------------------------- /Chapter01/example14/priorityordering.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: group variable priority ordering example play 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: show assigned group variables 8 | vars: 9 | msg: | 10 | http_port:{{ hostvars[inventory_hostname]['http_port'] }} 11 | thread_count:{{ hostvars[inventory_hostname]['thread_count'] | default("undefined") }} 12 | secure:{{ hostvars[inventory_hostname]['secure'] }} 13 | debug: 14 | msg: "{{ msg.split('\n') }}" 15 | -------------------------------------------------------------------------------- /Chapter01/example15/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook with the following command: 8 | 9 | ansible-playbook -i priority-hosts -c local priorityordering.yaml 10 | -------------------------------------------------------------------------------- /Chapter01/example15/priority-hosts: -------------------------------------------------------------------------------- 1 | [frontend] 2 | host1.example.com 3 | host2.example.com 4 | 5 | [web:children] 6 | frontend 7 | 8 | [web:vars] 9 | http_port=80 10 | secure=true 11 | 12 | [proxy] 13 | host1.example.com 14 | 15 | [proxy:vars] 16 | http_port=8080 17 | thread_count=10 18 | ansible_group_priority=10 19 | -------------------------------------------------------------------------------- /Chapter01/example15/priorityordering.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: group variable priority ordering example play 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: show assigned group variables 8 | vars: 9 | msg: | 10 | http_port:{{ hostvars[inventory_hostname]['http_port'] }} 11 | thread_count:{{ hostvars[inventory_hostname]['thread_count'] | default("undefined") }} 12 | secure:{{ hostvars[inventory_hostname]['secure'] }} 13 | debug: 14 | msg: "{{ msg.split('\n') }}" 15 | -------------------------------------------------------------------------------- /Chapter02/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this command: 8 | 9 | ansible-vault create --vault-id @prompt secrets.yaml 10 | 11 | Then insert the following data into the Vault: 12 | 13 | --- 14 | my_secret: is_safe 15 | 16 | You can try and read the resulting file with: 17 | 18 | cat secrets.yaml 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter02/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Create a password file for your new Vault using the command: 8 | 9 | echo "my long password" > password_file 10 | 11 | Then run this command to create the vault: 12 | 13 | ansible-vault create --vault-id ./password_file more_secrets.yaml 14 | 15 | Later try decrypting this with: 16 | 17 | ansible-vault decrypt --vault-id ./password_file more_secrets.yaml 18 | 19 | Check the contents to ensure decryption was successful. 20 | -------------------------------------------------------------------------------- /Chapter02/example02/password_file: -------------------------------------------------------------------------------- 1 | my long password 2 | -------------------------------------------------------------------------------- /Chapter02/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Create a password script for your new Vault called `password.sh` that contains: 8 | 9 | #!/bin/sh 10 | echo "a long password" 11 | 12 | Remember to make the script executable: 13 | 14 | chmod +x password.sh 15 | 16 | Then run this command to create the vault: 17 | 18 | ansible-vault create --vault-id ./password.sh even_more_secrets.yaml 19 | 20 | Try editing it and manually entering the password from the script above using the command: 21 | 22 | ansible-vault edit --vault-id @prompt even_more_secrets.yaml 23 | 24 | Later try re-keying this with the command: 25 | 26 | ansible-vault rekey --vault-id password.sh --new-vault-id dev@./password_file even_more_secrets.yaml 27 | 28 | -------------------------------------------------------------------------------- /Chapter02/example03/password.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo "a long password" 3 | -------------------------------------------------------------------------------- /Chapter02/example03/password_file: -------------------------------------------------------------------------------- 1 | my long password 2 | -------------------------------------------------------------------------------- /Chapter02/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Ensure `password.sh` is executable: 8 | 9 | chmod +x password.sh 10 | 11 | Try encrypting the included vars file using the command: 12 | 13 | ansible-vault encrypt --vault-id ./password.sh a_vars_file.yaml 14 | 15 | Check the contents: 16 | 17 | cat a_vars_file.yaml 18 | 19 | -------------------------------------------------------------------------------- /Chapter02/example04/a_vars_file.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | something: "better than nothing" 3 | -------------------------------------------------------------------------------- /Chapter02/example04/password.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo "a long password" 3 | -------------------------------------------------------------------------------- /Chapter02/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Ensure `password.sh` is executable: 8 | 9 | chmod +x password.sh 10 | 11 | Try encrypting the included vars file using the command: 12 | 13 | ansible-vault encrypt --vault-id ./password.sh a_vars_file.yaml 14 | 15 | Now run the playbook: 16 | 17 | ansible-playbook -i mastery-hosts --vault-id ./password.sh showme.yaml 18 | 19 | -------------------------------------------------------------------------------- /Chapter02/example05/a_vars_file.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | something: "better than nothing" 3 | -------------------------------------------------------------------------------- /Chapter02/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter02/example05/password.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo "a long password" 3 | -------------------------------------------------------------------------------- /Chapter02/example05/showme.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: show me an encrypted var 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars_files: 7 | - a_vars_file.yaml 8 | 9 | tasks: 10 | - name: print the variable 11 | debug: 12 | var: something 13 | -------------------------------------------------------------------------------- /Chapter02/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run the playbook as follows: 8 | 9 | ansible-playbook -i mastery-hosts inline.yaml 10 | 11 | Then generate the encrypted variable to go inline as per the book: 12 | 13 | ansible-vault encrypt_string --vault-id test@./password.sh "secure_password" --name my_secret 14 | 15 | Replace the variable definition in the playbook with the output of this command, then run the playbook again this time with the appropriate Vault ID to enable decryption: 16 | 17 | ansible-playbook -i mastery-hosts --vault-id test@./password.sh inline.yaml 18 | -------------------------------------------------------------------------------- /Chapter02/example06/inline.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: inline secret variable demonstration 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars: 7 | my_secret: secure_password 8 | 9 | tasks: 10 | - name: print the secure variable 11 | debug: 12 | var: my_secret 13 | 14 | -------------------------------------------------------------------------------- /Chapter02/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter02/example06/password.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo "a long password" 3 | -------------------------------------------------------------------------------- /Chapter02/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Ensure `password.sh` is executable: 8 | 9 | chmod +x password.sh 10 | 11 | Try encrypting the included vars file using the command: 12 | 13 | ansible-vault encrypt --vault-id ./password.sh a_vars_file.yaml 14 | 15 | Now try running the playbook: 16 | 17 | ansible-playbook -i mastery-hosts --vault-id password.sh showme.yaml -v 18 | -------------------------------------------------------------------------------- /Chapter02/example07/a_vars_file.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | something: "better than nothing" 3 | -------------------------------------------------------------------------------- /Chapter02/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter02/example07/password.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo "a long password" 3 | -------------------------------------------------------------------------------- /Chapter02/example07/showme.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: show me an encrypted var 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars_files: 7 | - a_vars_file.yaml 8 | 9 | tasks: 10 | - name: print the variable 11 | debug: 12 | var: something 13 | no_log: true 14 | -------------------------------------------------------------------------------- /Chapter03/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - Windows 10 build 17763 5 | 6 | Run this command: 7 | 8 | Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux 9 | 10 | If installing Linux without the aid of the Windows Store: 11 | 12 | Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu.appx -UseBasicParsing 13 | 14 | Rename-Item Ubuntu.appx Ubuntu.zip 15 | Expand-Archive Ubuntu.zip C:\WSL\Ubuntu 16 | 17 | C:\WSL\Ubuntu\ubuntu.exe 18 | 19 | 20 | -------------------------------------------------------------------------------- /Chapter03/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - Windows Server 2016 Standard Edition build 14393 5 | 6 | Ensure the WinRM Python module is installed on your Ansible host: 7 | 8 | sudo yum install python2-winrm 9 | 10 | or: 11 | 12 | sudo pip install "pywinrm>=0.3.0" 13 | 14 | Edit the inventory to suit your environment (password, IP address of Windows host) and then run the following command to test connectivity: 15 | 16 | ansible -i windows-hosts -m win_ping all 17 | 18 | -------------------------------------------------------------------------------- /Chapter03/example03/windows-hosts: -------------------------------------------------------------------------------- 1 | [windows] 2 | 192.168.81.150 3 | 4 | [windows:vars] 5 | ansible_user=Administrator 6 | ansible_password="password" 7 | ansible_port=5986 8 | ansible_connection=winrm 9 | ansible_winrm_server_cert_validation=ignore 10 | -------------------------------------------------------------------------------- /Chapter03/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - Windows Server 2016 Standard Edition build 14393 5 | - CentOS 7.6 6 | 7 | Installing Kerberos support on the Ansible host: 8 | 9 | sudo yum -y install python-devel krb5-devel krb5-libs krb5-workstation 10 | 11 | Install the required Python support: 12 | 13 | sudo yum install python-pip gcc 14 | sudo pip install pywinrm[kerberos] 15 | 16 | Edit `/etc/krb5.conf` as detailed in the book. Then test with (modify to suit your Active Directory domain): 17 | 18 | kinit Administrator@MASTERY.EXAMPLE.COM 19 | klist 20 | 21 | Modify the enclosed inventory to suit your environment. Then test connectivity with: 22 | 23 | ansible -i windows-hosts -m win_ping all 24 | 25 | -------------------------------------------------------------------------------- /Chapter03/example04/windows-hosts: -------------------------------------------------------------------------------- 1 | [windows] 2 | 192.168.81.150 3 | 4 | [windows:vars] 5 | ansible_user=administrator@MASTERY.EXAMPLE.COM 6 | ansible_password="password" 7 | ansible_port=5986 8 | ansible_connection=winrm 9 | ansible_winrm_server_cert_validation=ignore 10 | -------------------------------------------------------------------------------- /Chapter03/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - Windows Server 2016 Standard Edition build 14393 5 | 6 | Run this command: 7 | 8 | winrm configSDDL default 9 | -------------------------------------------------------------------------------- /Chapter03/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - Windows Server 2016 Standard Edition build 14393 5 | - CentOS 7.6 6 | 7 | Import your CA signed certificate into the Windows Certificate store: 8 | 9 | Import-Certificate -FilePath .\certnew.cer -CertStoreLocation Cert:\LocalMachine\My 10 | 11 | Delete any previously created WinRM listeners: 12 | 13 | winrm delete winrm/config/Listener?Address=*+Transport=HTTPS 14 | 15 | Create a new HTTPS listener referring to the new CA signed certificate: 16 | 17 | New-Item -Path WSMan:\Localhost\Listener -Transport HTTPS -Address * -CertificateThumbprint 18 | 19 | Place a copy of the Base64 encoded CA certificate for the above in `/etc/pki/ca-trust/source/anchors/` on the Ansible host. Then run: 20 | 21 | sudo update-ca-trust enable 22 | sudo update-ca-trust extract 23 | 24 | Finally, update the enclosed inventory with the correct details for your Windows host and run the following command to test Ansible connectivity: 25 | 26 | ansible -i windows-hosts -m win_ping all 27 | -------------------------------------------------------------------------------- /Chapter03/example06/windows-hosts: -------------------------------------------------------------------------------- 1 | [windows] 2 | WIN-2NJFMR0MNBD.mastery.example.com 3 | 4 | [windows:vars] 5 | ansible_user=administrator@MASTERY.EXAMPLE.COM 6 | ansible_password="password" 7 | ansible_port=5986 8 | ansible_connection=winrm 9 | ansible_winrm_ca_trust_path=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 10 | -------------------------------------------------------------------------------- /Chapter03/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - Windows Server 2016 Standard Edition build 14393 5 | - CentOS 7.6 6 | 7 | The enclosed playbook contains the file related code snippets from the Windows chapter: 8 | 9 | ansible-playbook -i windows-hosts win_file.yaml 10 | 11 | -------------------------------------------------------------------------------- /Chapter03/example07/mastery.txt: -------------------------------------------------------------------------------- 1 | Hello World! 2 | -------------------------------------------------------------------------------- /Chapter03/example07/win_file.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Windows file example playbook 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: Create temporary directory 8 | win_file: 9 | path: 'C:\Mastery Test' 10 | state: directory 11 | 12 | - name: Copy across a test file 13 | win_copy: 14 | src: mastery.txt 15 | dest: 'C:\Mastery Test\mastery.txt' 16 | 17 | - name: Create a directory using PowerShell 18 | win_shell: New-Item -Path C:\Mastery -ItemType Directory 19 | 20 | - name: Create a directory using cmd.exe 21 | win_shell: mkdir C:\MasteryCMD 22 | args: 23 | executable: cmd 24 | -------------------------------------------------------------------------------- /Chapter03/example07/windows-hosts: -------------------------------------------------------------------------------- 1 | [windows] 2 | WIN-2NJFMR0MNBD.mastery.example.com 3 | 4 | [windows:vars] 5 | ansible_user=administrator@MASTERY.EXAMPLE.COM 6 | ansible_password="password" 7 | ansible_port=5986 8 | ansible_connection=winrm 9 | ansible_winrm_ca_trust_path=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 10 | -------------------------------------------------------------------------------- /Chapter04/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Below are the commands referenced in the book to install AWX and enable HTTPS support: 8 | 9 | git clone https://github.com/ansible/awx.git 10 | 11 | cd awx/installer 12 | 13 | sudo ansible-playbook -i inventory install.yml 14 | 15 | sudo yum install nginx 16 | 17 | openssl req -x509 -nodes -newkey rsa:4096 -keyout /etc/pki/tls/private/mastery.example.com.key -out /etc/pki/tls/certs/mastery.example.com.crt -days 3650 -subj "/C=GB/CN=mastery.example.com" 18 | 19 | Change the default listening port in `nginx.conf`: 20 | 21 | server { 22 | listen 81 default_server; 23 | listen [::]:81 default_server; 24 | 25 | Enable and start the service: 26 | 27 | sudo systemctl enable nginx.service 28 | sudo systemctl start nginx.service 29 | 30 | Allow HTTPS through the firewall: 31 | 32 | sudo firewall-cmd --permanent --add-service=https 33 | sudo firewall-cmd --reload 34 | 35 | -------------------------------------------------------------------------------- /Chapter04/example01/awx.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 443 ssl; 3 | server_name awx.domain.com; 4 | 5 | ssl on; 6 | ssl_certificate /etc/pki/tls/certs/mastery.example.com.crt; 7 | ssl_certificate_key /etc/pki/tls/private/mastery.example.com.key; 8 | 9 | location / { 10 | proxy_pass http://127.0.0.1:80; 11 | proxy_set_header Host $host; 12 | proxy_set_header X-Real-IP $remote_addr; 13 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 14 | proxy_set_header X-Forwarded-Proto $scheme; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter04/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | With AWX installed, create a directory: 8 | 9 | sudo mkdir /var/lib/awx/projects/mastery 10 | 11 | Copy the included `example.yaml` into this directory: 12 | 13 | sudo cp example.yaml /var/lib/awx/projects/mastery 14 | -------------------------------------------------------------------------------- /Chapter04/example02/example.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: AWX example playbook 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: Create temporary directory 8 | file: 9 | path: /tmp/mastery 10 | state: directory 11 | 12 | - name: Create a file with example text 13 | lineinfile: 14 | path: /tmp/mastery/mastery.txt 15 | line: 'Created with Ansible Mastery!' 16 | create: yes 17 | -------------------------------------------------------------------------------- /Chapter05/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example01/demo.j2: -------------------------------------------------------------------------------- 1 | setting = {{ setting }} 2 | {% if feature.enabled %} 3 | feature = True 4 | {% else %} 5 | feature = False 6 | {% endif %} 7 | another_setting = {{ another_setting }} 8 | -------------------------------------------------------------------------------- /Chapter05/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example01/template-demo.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | setting: a_val 7 | feature: 8 | enabled: true 9 | another_setting: b_val 10 | tasks: 11 | - name: pause with render 12 | pause: 13 | prompt: "{{ lookup('template', 'demo.j2') }}" 14 | -------------------------------------------------------------------------------- /Chapter05/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example02/demo.j2: -------------------------------------------------------------------------------- 1 | setting = {{ setting }} 2 | {% if feature.enabled %} 3 | feature = True 4 | {% else %} 5 | feature = False 6 | {% endif %} 7 | another_setting = {{ another_setting }} 8 | -------------------------------------------------------------------------------- /Chapter05/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example02/template-demo.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | setting: a_val 7 | feature: 8 | enabled: false 9 | another_setting: b_val 10 | tasks: 11 | - name: pause with render 12 | pause: 13 | prompt: "{{ lookup('template', 'demo.j2') }}" 14 | -------------------------------------------------------------------------------- /Chapter05/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-v2.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example03/template-demo-v2.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | api: 7 | v2: true 8 | tasks: 9 | - name: pause with render 10 | debug: 11 | msg: "API = cinder{{ 'v2' if api.v2 else '' }}" 12 | -------------------------------------------------------------------------------- /Chapter05/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-v2.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example04/template-demo-v2.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | api: 7 | v2: false 8 | tasks: 9 | - name: pause with render 10 | debug: 11 | msg: "API = cinder{{ 'v2' if api.v2 else '' }}" 12 | -------------------------------------------------------------------------------- /Chapter05/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-for.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example05/demo-for.j2: -------------------------------------------------------------------------------- 1 | # data dirs 2 | {% for dir in data_dirs -%} 3 | data_dir = {{ dir }} 4 | {% else -%} 5 | # no data dirs found 6 | {% endfor -%} 7 | 8 | -------------------------------------------------------------------------------- /Chapter05/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example05/template-demo-for.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: [] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-for.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-for.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example06/demo-for.j2: -------------------------------------------------------------------------------- 1 | # data dirs 2 | {% for dir in data_dirs if dir != "/" -%} 3 | {% if loop.first -%} 4 | data_dir = {{ dir }}, 5 | {% else -%} 6 | {{ dir }}, 7 | {% endif -%} 8 | {% else -%} 9 | # no data dirs found 10 | {% endfor -%} 11 | -------------------------------------------------------------------------------- /Chapter05/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example06/template-demo-for.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-for.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-for.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example07/demo-for.j2: -------------------------------------------------------------------------------- 1 | # data dirs. 2 | {% for dir in data_dirs if dir != "/" -%} 3 | {% if loop.first -%} 4 | data_dir = {{ dir }}{{ ',' if not loop.last else '' }} 5 | {% else -%} 6 | {{ dir }}{{ ',' if not loop.last else '' }} 7 | {% endif -%} 8 | {% else -%} 9 | # no data dirs found 10 | {% endfor -%} 11 | -------------------------------------------------------------------------------- /Chapter05/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example07/template-demo-for.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-for.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example08/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test() -%} 2 | {{ test.name }} 3 | {%- endmacro -%} 4 | {{ test() }} 5 | -------------------------------------------------------------------------------- /Chapter05/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example08/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example09/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test(var_a='a string') -%} 2 | {{ test.arguments }} 3 | {%- endmacro -%} 4 | {{ test() }} 5 | -------------------------------------------------------------------------------- /Chapter05/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example09/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example10/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test(var_a='a string') -%} 2 | {{ test.arguments }} 3 | {{ test.defaults }} 4 | {%- endmacro -%} 5 | {{ test() }} 6 | -------------------------------------------------------------------------------- /Chapter05/example10/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example10/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example11/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example11/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test() -%} 2 | {{ kwargs }} 3 | {{ test.catch_kwargs }} 4 | {%- endmacro -%} 5 | {{ test(unexpected='surprise') }} 6 | -------------------------------------------------------------------------------- /Chapter05/example11/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example11/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example12/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example12/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test() -%} 2 | {{ varargs }} 3 | {{ test.catch_varargs }} 4 | {%- endmacro -%} 5 | {{ test('surprise') }} 6 | -------------------------------------------------------------------------------- /Chapter05/example12/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example12/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example13/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example13/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test() -%} 2 | The text from the caller follows: 3 | {{ caller() }} 4 | {%- endmacro -%} 5 | {% call test() -%} 6 | This is text inside the call 7 | {% endcall -%} 8 | -------------------------------------------------------------------------------- /Chapter05/example13/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example13/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example14/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-macro.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example14/demo-macro.j2: -------------------------------------------------------------------------------- 1 | {% macro test(group, hosts) -%} 2 | [{{ group }}] 3 | {% for host in hosts -%} 4 | {{ host }} {{ caller(host) }} 5 | {%- endfor -%} 6 | {%- endmacro -%} 7 | {% call(host) test('web', ['host1', 'host2', 'host3']) -%} 8 | ssh_host_name={{ host }}.example.name ansible_sudo=true 9 | {% endcall -%} 10 | {% call(host) test('db', ['db1', 'db2']) %} 11 | ssh_host_name={{ host }}.example.name 12 | {% endcall -%} 13 | 14 | -------------------------------------------------------------------------------- /Chapter05/example14/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example14/template-demo-macro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | data_dirs: ['/', '/foo', '/bar'] 7 | tasks: 8 | - name: pause with render 9 | pause: 10 | prompt: "{{ lookup('template', 'demo-macro.j2') }}" 11 | -------------------------------------------------------------------------------- /Chapter05/example15/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example15/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example15/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the template 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | answers: "no so YES no" 7 | tasks: 8 | - name: debug the template 9 | debug: 10 | msg: "{{ answers | replace('no', 'yes') | lower }}" 11 | 12 | -------------------------------------------------------------------------------- /Chapter05/example16/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example16/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example16/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | tasks: 6 | - name: fail a task 7 | debug: 8 | msg: "I am not a change" 9 | register: derp 10 | - name: only do this on change 11 | debug: 12 | msg: "You had a change" 13 | when: derp is changed 14 | - name: only do this on success 15 | debug: 16 | msg: "You had a success" 17 | when: derp is success 18 | 19 | -------------------------------------------------------------------------------- /Chapter05/example17/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example17/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example17/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | tasks: 6 | - name: shuffle the cards 7 | debug: 8 | msg: "{{ ['Ace', 'Queen', 'King', 'Deuce'] | shuffle }}" 9 | 10 | -------------------------------------------------------------------------------- /Chapter05/example18/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example18/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example18/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | tasks: 6 | - name: demo basename 7 | debug: 8 | msg: "{{ '/var/log/nova/nova-api.log' | basename }}" 9 | 10 | -------------------------------------------------------------------------------- /Chapter05/example19/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example19/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example19/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | tasks: 6 | - name: demo dirname 7 | debug: 8 | msg: "{{ '/var/log/nova/nova-api.log' | dirname }}" 9 | 10 | -------------------------------------------------------------------------------- /Chapter05/example20/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example20/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example20/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | tasks: 6 | - name: demo filter 7 | debug: 8 | msg: "{{ '~/.stackrc' | expanduser }}" 9 | 10 | -------------------------------------------------------------------------------- /Chapter05/example21/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-filters.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example21/derp: -------------------------------------------------------------------------------- 1 | Ansible is great 2 | -------------------------------------------------------------------------------- /Chapter05/example21/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example21/template-demo-filters.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | tasks: 6 | - name: read file 7 | slurp: 8 | src: derp 9 | register: derp 10 | - name: display file content (undecoded) 11 | debug: 12 | var: derp.content 13 | - name: display file content (decoded) 14 | debug: 15 | var: derp.content | b64decode 16 | 17 | -------------------------------------------------------------------------------- /Chapter05/example22/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-objects.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example22/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example22/template-demo-objects.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the filters 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: string methods 8 | debug: 9 | msg: "{{ 'foo bar baz'.upper().split() }}" 10 | -------------------------------------------------------------------------------- /Chapter05/example23/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts template-demo-comparisons.yaml 10 | -------------------------------------------------------------------------------- /Chapter05/example23/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/example23/template-demo-comparisons.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: demo the logic 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | num1: 10 7 | num3: 10 8 | tasks: 9 | - name: logic and comparison 10 | debug: 11 | msg: "Can you read me?" 12 | when: num1 >= num3 and num1 is even and num2 is not defined 13 | -------------------------------------------------------------------------------- /Chapter06/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example01/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: broken website 8 | uri: 9 | url: http://notahost.nodomain 10 | -------------------------------------------------------------------------------- /Chapter06/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example02/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: broken website 8 | uri: 9 | url: http://notahost.nodomain 10 | ignore_errors: true 11 | -------------------------------------------------------------------------------- /Chapter06/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - iscsi-initiator-utils-6.2.0.874-10 6 | - Ansible 2.7.5 7 | 8 | Run this playbook using the command: 9 | 10 | ansible-playbook -i mastery-hosts error.yaml 11 | -------------------------------------------------------------------------------- /Chapter06/example03/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: query sessions 8 | command: /sbin/iscsiadm -m session 9 | register: sessions 10 | -------------------------------------------------------------------------------- /Chapter06/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - iscsi-initiator-utils-6.2.0.874-10 6 | - Ansible 2.7.5 7 | 8 | Run this playbook using the command: 9 | 10 | ansible-playbook -i mastery-hosts error.yaml 11 | -------------------------------------------------------------------------------- /Chapter06/example04/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: query sessions 8 | command: /sbin/iscsiadm -m session 9 | register: sessions 10 | failed_when: sessions.rc not in (0, 21) 11 | -------------------------------------------------------------------------------- /Chapter06/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | To create the empty repository to be used for testing this playbook, you can run the following commands: 8 | 9 | mkdir /srv/app 10 | cd /srv/app 11 | git init 12 | git commit --allow-empty -m "initial commit" 13 | 14 | Run this playbook using the command: 15 | 16 | ansible-playbook -i mastery-hosts error.yaml -v 17 | -------------------------------------------------------------------------------- /Chapter06/example05/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: delete branch bad 8 | command: git branch -D badfeature 9 | args: 10 | chdir: /srv/app 11 | -------------------------------------------------------------------------------- /Chapter06/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | To create the empty repository to be used for testing this playbook, you can run the following commands: 8 | mkdir /srv/app 9 | cd /srv/app 10 | git init 11 | git commit --allow-empty -m "initial commit" 12 | 13 | Run this playbook using the command: 14 | 15 | ansible-playbook -i mastery-hosts error.yaml -v 16 | -------------------------------------------------------------------------------- /Chapter06/example06/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: check if branch badfeature exists 8 | command: git branch 9 | args: 10 | chdir: /srv/app 11 | register: branches 12 | - name: delete branch bad 13 | command: git branch -D badfeature 14 | args: 15 | chdir: /srv/app 16 | when: branches.stdout is search('badfeature') 17 | -------------------------------------------------------------------------------- /Chapter06/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | To create the empty repository to be used for testing this playbook, you can run the following commands: 8 | mkdir /srv/app 9 | cd /srv/app 10 | git init 11 | git commit --allow-empty -m "initial commit" 12 | 13 | Run this playbook using the command: 14 | 15 | ansible-playbook -i mastery-hosts error.yaml -v 16 | -------------------------------------------------------------------------------- /Chapter06/example07/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: delete branch bad 8 | command: git branch -D badfeature 9 | args: 10 | chdir: /srv/app 11 | register: gitout 12 | failed_when: 13 | - gitout.rc != 0 14 | - not gitout.stderr is search('branch.*not found') 15 | -------------------------------------------------------------------------------- /Chapter06/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | To create the empty repository to be used for testing this playbook, you can run the following commands: 8 | mkdir /srv/app 9 | cd /srv/app 10 | git init 11 | git commit --allow-empty -m "initial commit" 12 | 13 | Run this playbook using the command: 14 | 15 | ansible-playbook -i mastery-hosts error.yaml -v 16 | 17 | To test the playbook actually deleting a branch, create the branch using the commands: 18 | 19 | cd /srv/app 20 | git branch badfeature 21 | cd - 22 | ansible-playbook -i mastery-hosts error.yaml -v 23 | -------------------------------------------------------------------------------- /Chapter06/example08/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: delete branch bad 8 | command: git branch -D badfeature 9 | args: 10 | chdir: /srv/app 11 | register: gitout 12 | failed_when: 13 | - gitout.rc != 0 14 | - not gitout.stderr is search('branch.*not found') 15 | changed_when: gitout.rc == 0 16 | -------------------------------------------------------------------------------- /Chapter06/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example09/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: discover tango directory 8 | stat: path=/srv/whiskey/tango 9 | register: tango 10 | - name: run frobitz 11 | script: files/frobitz --initialize /srv/whiskey/tango 12 | when: not tango.stat.exists 13 | -------------------------------------------------------------------------------- /Chapter06/example09/files/frobitz: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm -rf /srv/whiskey/tango 3 | mkdir -p /srv/whiskey/tango 4 | -------------------------------------------------------------------------------- /Chapter06/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example10/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: run frobitz 8 | script: files/frobitz 9 | args: 10 | creates: /srv/whiskey/tango 11 | -------------------------------------------------------------------------------- /Chapter06/example10/files/frobitz: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm -rf /srv/whiskey/tango 3 | mkdir -p /srv/whiskey/tango 4 | -------------------------------------------------------------------------------- /Chapter06/example10/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example11/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - iscsi-initiator-utils-6.2.0.874-10 6 | - Ansible 2.7.5 7 | 8 | Run this playbook using the command: 9 | 10 | ansible-playbook -i mastery-hosts error.yaml 11 | -------------------------------------------------------------------------------- /Chapter06/example11/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: discover iscsi sessions 8 | command: /sbin/iscsiadm -m session 9 | register: sessions 10 | failed_when: 11 | - sessions.rc != 0 12 | - not sessions.stderr is 13 | search('No active sessions') 14 | changed_when: false 15 | -------------------------------------------------------------------------------- /Chapter06/example11/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example12/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example12/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - block: 8 | - name: delete branch bad 9 | command: git branch -D badfeature 10 | args: 11 | chdir: /srv/app 12 | 13 | - name: this task is lost 14 | debug: 15 | msg: "I do not get seen" 16 | 17 | rescue: 18 | - name: cleanup task 19 | debug: 20 | msg: "I am cleaning up" 21 | 22 | - name: cleanup task 2 23 | debug: 24 | msg: "I am also cleaning up" 25 | - name: task after block 26 | debug: 27 | msg: "Execution goes on" 28 | -------------------------------------------------------------------------------- /Chapter06/example12/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example13/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example13/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - block: 8 | - name: delete branch bad 9 | command: git branch -D badfeature 10 | args: 11 | chdir: /srv/app 12 | 13 | - name: this task is lost 14 | debug: 15 | msg: "I do not get seen" 16 | 17 | rescue: 18 | - name: cleanup task 19 | debug: 20 | msg: "I am cleaning up" 21 | 22 | - name: cleanup task 2 23 | debug: 24 | msg: "I am also cleaning up" 25 | always: 26 | - name: most important task 27 | debug: 28 | msg: "Never going to let you down" 29 | 30 | - name: task after block 31 | debug: 32 | msg: "Execution goes on" 33 | -------------------------------------------------------------------------------- /Chapter06/example13/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example14/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example14/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - block: 8 | - name: delete branch bad 9 | command: git branch -D badfeature 10 | args: 11 | chdir: /srv/app 12 | register: gitout 13 | failed_when: 14 | - gitout.rc != 0 15 | - not gitout.stderr is search('branch.*not found') 16 | 17 | - name: this task is lost 18 | debug: 19 | msg: "I do not get seen" 20 | 21 | rescue: 22 | - name: cleanup task 23 | debug: 24 | msg: "I am cleaning up" 25 | 26 | - name: cleanup task 2 27 | debug: 28 | msg: "I am also cleaning up" 29 | 30 | always: 31 | - name: most important task 32 | debug: 33 | msg: "Never going to let you down" 34 | 35 | - name: task after block 36 | debug: 37 | msg: "Execution goes on" 38 | -------------------------------------------------------------------------------- /Chapter06/example14/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example15/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example15/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: delete branch bad 8 | command: git branch -D badfeature 9 | args: 10 | chdir: /srv/app 11 | - name: important task 12 | debug: 13 | msg: It is important we attempt this task! 14 | -------------------------------------------------------------------------------- /Chapter06/example15/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example16/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts error.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example16/error.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: error handling 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: delete branch bad 8 | command: git branch -D badfeature 9 | args: 10 | chdir: /srv/app 11 | ignore_unreachable: true 12 | - name: important task 13 | debug: 14 | msg: It is important we attempt this task! 15 | -------------------------------------------------------------------------------- /Chapter06/example16/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example17/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts loop.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example17/loop.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: looping demo 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: create a directory 8 | file: 9 | path: /srv/whiskey/alpha 10 | state: directory 11 | - name: create another directory 12 | file: 13 | path: /srv/whiskey/beta 14 | state: directory 15 | -------------------------------------------------------------------------------- /Chapter06/example17/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/example18/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts loop.yaml 10 | -------------------------------------------------------------------------------- /Chapter06/example18/loop.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: looping demo 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: create a directory 8 | file: 9 | path: "{{ item }}" 10 | state: directory 11 | loop: 12 | - /srv/whiskey/alpha 13 | - /srv/whiskey/beta 14 | -------------------------------------------------------------------------------- /Chapter06/example18/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml 10 | -------------------------------------------------------------------------------- /Chapter07/example01/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: task inclusion 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: non-included task 8 | debug: 9 | msg: "I am not included" 10 | 11 | - include: more-tasks.yaml 12 | -------------------------------------------------------------------------------- /Chapter07/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example01/more-tasks.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: included task 1 3 | debug: 4 | msg: "I am the first included task" 5 | 6 | - name: included task 2 7 | debug: 8 | msg: "I am the second included task" 9 | -------------------------------------------------------------------------------- /Chapter07/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml 10 | -------------------------------------------------------------------------------- /Chapter07/example02/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: task inclusion 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: non-included task 8 | debug: 9 | msg: "I am not included" 10 | 11 | - include: more-tasks.yaml 12 | 13 | - name: after-included tasks 14 | debug: 15 | msg: "I run last" 16 | -------------------------------------------------------------------------------- /Chapter07/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example02/more-tasks.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: included task 1 3 | debug: 4 | msg: "I am the first included task" 5 | 6 | - name: included task 2 7 | debug: 8 | msg: "I am the second included task" 9 | -------------------------------------------------------------------------------- /Chapter07/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter07/example03/files.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: create leading path 3 | file: 4 | path: "{{ path }}" 5 | state: directory 6 | 7 | - name: touch the file 8 | file: 9 | path: "{{ path + '/' + file }}" 10 | state: touch 11 | -------------------------------------------------------------------------------- /Chapter07/example03/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: touch files 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - include: files.yaml 8 | vars: 9 | path: /tmp/foo 10 | file: herp 11 | 12 | - include: files.yaml 13 | vars: 14 | path: /tmp/foo 15 | file: derp 16 | -------------------------------------------------------------------------------- /Chapter07/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter07/example04/files.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: create leading path 3 | file: 4 | path: "{{ item.value.path }}" 5 | state: directory 6 | with_dict: "{{ files }}" 7 | 8 | - name: touch the file 9 | file: 10 | path: "{{ item.value.path + '/' + item.key }}" 11 | state: touch 12 | with_dict: "{{ files }}" 13 | -------------------------------------------------------------------------------- /Chapter07/example04/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: touch files 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - include: files.yaml 8 | vars: 9 | files: 10 | herp: 11 | path: /tmp/foo 12 | derp: 13 | path: /tmp/foo 14 | -------------------------------------------------------------------------------- /Chapter07/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter07/example05/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: task inclusion 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - include: more-tasks.yaml 8 | when: item | bool 9 | vars: 10 | a_list: 11 | - true 12 | - false 13 | -------------------------------------------------------------------------------- /Chapter07/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example05/more-tasks.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: included task 1 3 | debug: 4 | msg: "I am the first included task" 5 | with_items: "{{ a_list }}" 6 | 7 | - name: include task 2 8 | debug: 9 | msg: "I am the second included task" 10 | with_items: "{{ a_list }}" 11 | -------------------------------------------------------------------------------- /Chapter07/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the commands: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | ansible-playbook -i mastery-hosts includer.yaml -v --tags second 11 | -------------------------------------------------------------------------------- /Chapter07/example06/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: task inclusion 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - include: more-tasks.yaml 8 | vars: 9 | data: first 10 | tags: first 11 | 12 | - include: more-tasks.yaml 13 | vars: 14 | data: second 15 | tags: second 16 | -------------------------------------------------------------------------------- /Chapter07/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example06/more-tasks.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: included task 3 | debug: 4 | msg: "My data is {{ data }}" 5 | -------------------------------------------------------------------------------- /Chapter07/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter07/example07/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: task inclusion 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - include: more-tasks.yaml 8 | loop: 9 | - one 10 | - two 11 | -------------------------------------------------------------------------------- /Chapter07/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example07/more-tasks.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: included task 1 3 | debug: 4 | msg: "I am the first included task with {{ item }}" 5 | - name: included task 2 6 | debug: 7 | msg: "I am the second included task with {{ item }}" 8 | -------------------------------------------------------------------------------- /Chapter07/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter07/example08/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: task inclusion 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - include: more-tasks.yaml 8 | loop: 9 | - one 10 | - two 11 | loop_control: 12 | loop_var: include_item 13 | -------------------------------------------------------------------------------- /Chapter07/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example08/more-tasks.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: included task 1 3 | debug: 4 | msg: "I combine {{ item }} and {{ include_item }}" 5 | with_items: 6 | - a 7 | - b 8 | -------------------------------------------------------------------------------- /Chapter07/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the commands: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | ansible-playbook -i mastery-hosts includer.yaml -v -e foo=false 11 | -------------------------------------------------------------------------------- /Chapter07/example09/handlers.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: a handler 3 | debug: 4 | msg: "handling a thing" 5 | -------------------------------------------------------------------------------- /Chapter07/example09/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: touch files 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: a task 8 | debug: 9 | msg: "I am a changing task" 10 | changed_when: true 11 | notify: a handler 12 | 13 | handlers: 14 | - include: handlers.yaml 15 | when: foo | default('true') | bool 16 | -------------------------------------------------------------------------------- /Chapter07/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter07/example10/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: vars 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars_files: 7 | - variables.yaml 8 | 9 | tasks: 10 | - name: a task 11 | debug: 12 | msg: "I am a {{ varname }}" 13 | -------------------------------------------------------------------------------- /Chapter07/example10/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example10/variables.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | varname: derp 3 | -------------------------------------------------------------------------------- /Chapter07/example11/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v -e varfile=variables.yaml 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/example11/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: vars 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | vars_files: 7 | - "{{ varfile }}" 8 | 9 | tasks: 10 | - name: a task 11 | debug: 12 | msg: "I am a {{ varname }}" 13 | -------------------------------------------------------------------------------- /Chapter07/example11/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example11/variables.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | varname: derp 3 | -------------------------------------------------------------------------------- /Chapter07/example12/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v -e varfile=variables.yaml 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/example12/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: vars 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: load variables 8 | include_vars: "{{ varfile }}" 9 | 10 | - name: a task 11 | debug: 12 | msg: "I am a {{ varname }}" 13 | -------------------------------------------------------------------------------- /Chapter07/example12/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example12/variables.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | varname: derp 3 | -------------------------------------------------------------------------------- /Chapter07/example13/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts -c local includer.yaml -v 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/example13/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: vars 3 | hosts: localhost 4 | gather_facts: true 5 | 6 | tasks: 7 | - name: load variables 8 | include_vars: "{{ item }}" 9 | with_first_found: 10 | - "{{ ansible_distribution }}.yaml" 11 | - "{{ ansible_os_family }}.yaml" 12 | - variables.yaml 13 | 14 | - name: a task 15 | debug: 16 | msg: "I am a {{ varname }}" 17 | -------------------------------------------------------------------------------- /Chapter07/example13/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example13/variables.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | varname: derp 3 | -------------------------------------------------------------------------------- /Chapter07/example14/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts includer.yaml -v -e @variables.yaml 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/example14/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: vars 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: a task 8 | debug: 9 | msg: "I am a {{ varname }}" 10 | -------------------------------------------------------------------------------- /Chapter07/example14/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example14/variables.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | varname: derp 3 | -------------------------------------------------------------------------------- /Chapter07/example15/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the commands: 8 | 9 | ansible-playbook -i mastery-hosts includeme.yaml 10 | ansible-playbook -i mastery-hosts includer.yaml 11 | 12 | -------------------------------------------------------------------------------- /Chapter07/example15/includeme.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: include playbook 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: an included playbook task 8 | debug: 9 | msg: "I am in the included playbook" 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/example15/includer.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: include playbook 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: a task 8 | debug: 9 | msg: "I am in the main playbook" 10 | - name: include a playbook 11 | import_playbook: includeme.yaml 12 | 13 | -------------------------------------------------------------------------------- /Chapter07/example15/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example16/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts roleplay.yaml 10 | -------------------------------------------------------------------------------- /Chapter07/example16/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example16/roleplay.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | gather_facts: false 4 | 5 | roles: 6 | - role: simple 7 | -------------------------------------------------------------------------------- /Chapter07/example16/roles/simple/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | derp: herp 3 | -------------------------------------------------------------------------------- /Chapter07/example16/roles/simple/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: print a variable 3 | debug: 4 | var: derp 5 | -------------------------------------------------------------------------------- /Chapter07/example17/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts roleplay.yaml 10 | -------------------------------------------------------------------------------- /Chapter07/example17/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example17/roleplay.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | gather_facts: false 4 | 5 | roles: 6 | - role: simple 7 | derp: newval 8 | -------------------------------------------------------------------------------- /Chapter07/example17/roles/simple/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | derp: herp 3 | -------------------------------------------------------------------------------- /Chapter07/example17/roles/simple/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: print a variable 3 | debug: 4 | var: derp 5 | -------------------------------------------------------------------------------- /Chapter07/example18/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts roleplay.yaml 10 | -------------------------------------------------------------------------------- /Chapter07/example18/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/example18/roleplay.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | gather_facts: false 4 | 5 | pre_tasks: 6 | - name: pretask 7 | debug: 8 | msg: "a pre task" 9 | changed_when: true 10 | notify: say hi 11 | 12 | roles: 13 | - role: simple 14 | derp: newval 15 | 16 | tasks: 17 | - name: task 18 | debug: 19 | msg: "a task" 20 | changed_when: true 21 | notify: say hi 22 | 23 | post_tasks: 24 | - name: posttask 25 | debug: 26 | msg: "a post task" 27 | changed_when: true 28 | notify: say hi 29 | 30 | handlers: 31 | - name: say hi 32 | debug: 33 | msg: "hi" 34 | -------------------------------------------------------------------------------- /Chapter07/example18/roles/simple/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | derp: herp 3 | -------------------------------------------------------------------------------- /Chapter07/example18/roles/simple/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: print a variable 3 | debug: 4 | var: derp 5 | changed_when: true 6 | notify: say hi 7 | -------------------------------------------------------------------------------- /Chapter07/example18/roles/simple/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: print a variable 3 | debug: 4 | var: derp 5 | -------------------------------------------------------------------------------- /Chapter07/example19/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | The examples in this part of the chapter use the following commands: 8 | 9 | mkdir roles/ 10 | ansible-galaxy install -p roles angstwad.docker_ubuntu 11 | ansible-galaxy list -p roles/ 12 | ansible-galaxy info -p roles/ angstwad.docker_ubuntu | head -n 25 13 | cat roles/angstwad.docker_ubuntu/meta/main.yml 14 | ansible-galaxy init --help 15 | ansible-galaxy init --init-path roles/ autogen 16 | tree roles/autogen 17 | 18 | The following commands are theoretical - modify the repository URL and parameters to test: 19 | 20 | ansible-galaxy install -p /opt/ansible/roles git+git@git.internal.site:ansible-roles/foowhiz 21 | ansible-galaxy install -p /opt/ansible/roles git+git@git.internal.site:ansible-roles/foowhiz,v1 22 | ansible-galaxy install -p /opt/ansible/roles git+git@git.internal.site:ansible-roles/foowhiz,,foo-whiz-common 23 | ansible-galaxy install -r foowhiz-reqs.yaml 24 | 25 | -------------------------------------------------------------------------------- /Chapter08/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts vintro.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter08/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example01/vintro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: variable introspection demo 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: do a thing 8 | uri: 9 | url: https://derpops.bike 10 | register: derpops 11 | 12 | - name: show derpops 13 | debug: 14 | msg: "derpops value is {{ derpops }}" 15 | 16 | -------------------------------------------------------------------------------- /Chapter08/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts vintro.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter08/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example02/vintro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: variable introspection demo 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: do a thing 8 | uri: 9 | url: https://derpops.bike 10 | register: derpops 11 | 12 | - name: show derpops 13 | debug: 14 | var: derpops.server 15 | 16 | -------------------------------------------------------------------------------- /Chapter08/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts vintro.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter08/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example03/vintro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: variable introspection demo 3 | hosts: localhost 4 | 5 | tasks: 6 | - name: show a complex hash 7 | debug: 8 | var: ansible_python 9 | 10 | -------------------------------------------------------------------------------- /Chapter08/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts vintro.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter08/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example04/vintro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: variable introspection demo 3 | hosts: localhost 4 | 5 | tasks: 6 | - name: show a complex hash 7 | debug: 8 | var: ansible_python.version_info[-1] 9 | 10 | -------------------------------------------------------------------------------- /Chapter08/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts objmethod.yaml 10 | -------------------------------------------------------------------------------- /Chapter08/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example05/objmethod.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: sub-element access styles 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | - derp: 7 | keys: 8 | - c 9 | - d 10 | tasks: 11 | - name: subscript style 12 | debug: 13 | var: derp['keys'] 14 | - name: dot notation style 15 | debug: 16 | var: derp.keys 17 | 18 | -------------------------------------------------------------------------------- /Chapter08/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts objmethod.yaml 10 | 11 | In the interactive debug session, run the following commands (full explanation of these commands is in the accompanying book): 12 | 13 | p task 14 | p task.args 15 | task_vars['derp']['missing'] = "the end" 16 | r 17 | 18 | -------------------------------------------------------------------------------- /Chapter08/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example06/objmethod.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: sub-element access styles 3 | hosts: localhost 4 | gather_facts: false 5 | strategy: debug 6 | 7 | vars: 8 | - derp: 9 | keys: 10 | - c 11 | - d 12 | 13 | tasks: 14 | - name: subscript style 15 | debug: 16 | var: derp['keys'] 17 | 18 | - name: failing task 19 | debug: 20 | msg: "this is {{ derp['missing'] }}" 21 | 22 | - name: final task 23 | debug: 24 | msg: "my only friend the end" 25 | 26 | -------------------------------------------------------------------------------- /Chapter08/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | This example requires you to edit a core Ansible file - it might be worth making a backup before proceeding. 8 | 9 | sudo vim /usr/lib/python2.7/site-packages/ansible/inventory/manager.py 10 | 11 | Add the following line just below `def parse_sources...` 12 | 13 | import pdb; pdb.set_trace() 14 | 15 | Now run the playbook as we did before: 16 | 17 | ansible-playbook -i mastery-hosts objmethod.yaml 18 | 19 | Try out the following commands in the interactive `pdb` session: 20 | 21 | where 22 | list 23 | p self._sources 24 | next 25 | step 26 | continue 27 | 28 | -------------------------------------------------------------------------------- /Chapter08/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example07/objmethod.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: sub-element access styles 3 | hosts: localhost 4 | gather_facts: false 5 | strategy: debug 6 | 7 | vars: 8 | - derp: 9 | keys: 10 | - c 11 | - d 12 | 13 | tasks: 14 | - name: subscript style 15 | debug: 16 | var: derp['keys'] 17 | 18 | - name: failing task 19 | debug: 20 | msg: "this is {{ derp['missing'] }}" 21 | 22 | - name: final task 23 | debug: 24 | msg: "my only friend the end" 25 | 26 | -------------------------------------------------------------------------------- /Chapter08/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | This example requires you to edit a core Ansible file - it might be worth making a backup before proceeding. Also be sure to reverse any changes you made in the previous example before attempting this one. 8 | 9 | sudo vim /usr/lib/python2.7/site-packages/ansible/playbook/__init__.py 10 | 11 | Add the following line just below the for loop for `get_all_plugin_loaders` as shown in the book: 12 | 13 | import pdb; pdb.set_trace() 14 | 15 | Now run the playbook as we did before: 16 | 17 | ansible-playbook -i mastery-hosts objmethod.yaml 18 | 19 | Test out the interactive debug session as described in the book. 20 | 21 | Reverse all changes to the Ansible core files when complete. 22 | -------------------------------------------------------------------------------- /Chapter08/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example08/objmethod.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: sub-element access styles 3 | hosts: localhost 4 | gather_facts: false 5 | strategy: debug 6 | 7 | vars: 8 | - derp: 9 | keys: 10 | - c 11 | - d 12 | 13 | tasks: 14 | - name: subscript style 15 | debug: 16 | var: derp['keys'] 17 | 18 | - name: failing task 19 | debug: 20 | msg: "this is {{ derp['missing'] }}" 21 | 22 | - name: final task 23 | debug: 24 | msg: "my only friend the end" 25 | 26 | -------------------------------------------------------------------------------- /Chapter08/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | This example requires you to edit a core Ansible file - it might be worth making a backup before proceeding. Also be sure to reverse any changes you made in the previous example before attempting this one. 8 | 9 | sudo vim /usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py 10 | 11 | Add the following line just above the call to `check_for_controlpersist` as shown in the book: 12 | 13 | import pdb; pdb.set_trace() 14 | 15 | Now run the playbook as we did before: 16 | 17 | ansible-playbook -i mastery-hosts objmethod.yaml 18 | 19 | Test out the interactive debug session as described in the book. The commands given in the book are: 20 | 21 | step 22 | list 23 | l 24 | n 25 | n 26 | n 27 | n 28 | n 29 | n 30 | n 31 | n 32 | n 33 | n 34 | n 35 | p err 36 | 37 | Reverse all changes to the Ansible core files when complete. 38 | -------------------------------------------------------------------------------- /Chapter08/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/example09/objmethod.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: sub-element access styles 3 | hosts: localhost 4 | gather_facts: false 5 | strategy: debug 6 | 7 | vars: 8 | - derp: 9 | keys: 10 | - c 11 | - d 12 | 13 | tasks: 14 | - name: subscript style 15 | debug: 16 | var: derp['keys'] 17 | 18 | - name: failing task 19 | debug: 20 | msg: "this is {{ derp['missing'] }}" 21 | 22 | - name: final task 23 | debug: 24 | msg: "my only friend the end" 25 | 26 | -------------------------------------------------------------------------------- /Chapter08/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Edit this core Ansible file (this time by copying to a local `library/` directory first): 8 | 9 | mkdir library 10 | cp /usr/lib/python2.7/site-packages/ansible/modules/system/systemd.py library 11 | 12 | Insert the following line as shown in the accompanying book: 13 | 14 | import rpdb; rpdb.set_trace(addr="0.0.0.0") 15 | 16 | Edit the accompanying inventory to match your test host, then run the playbook: 17 | 18 | ansible-playbook -i mastery-debug rpdb.yaml -vv 19 | 20 | Once the playbook is running, it will appear to hang. At this point you can telnet into the remote host on port 4444 and use rpdb as described in the book. 21 | 22 | -------------------------------------------------------------------------------- /Chapter08/example10/mastery-debug: -------------------------------------------------------------------------------- 1 | debug.example.com ansible_host=192.168.81.154 2 | -------------------------------------------------------------------------------- /Chapter08/example10/rpdb.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: remote code debug 3 | hosts: debug.example.com 4 | gather_facts: false 5 | become: true 6 | 7 | tasks: 8 | - name: a remote module execution 9 | systemd: 10 | name: nginx 11 | state: stopped 12 | enabled: no 13 | -------------------------------------------------------------------------------- /Chapter09/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_module.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter09/example01/library/remote_copy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | import shutil 4 | def main(): 5 | module = AnsibleModule( 6 | argument_spec = dict( 7 | source=dict(required=True, type='str'), 8 | dest=dict(required=True, type='str') 9 | ) 10 | ) 11 | shutil.copy(module.params['source'], 12 | module.params['dest']) 13 | module.exit_json(changed=True) 14 | from ansible.module_utils.basic import * 15 | if __name__ == '__main__': 16 | main() 17 | 18 | -------------------------------------------------------------------------------- /Chapter09/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example01/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | -------------------------------------------------------------------------------- /Chapter09/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-doc -M library/ remote_copy | cat - 10 | -------------------------------------------------------------------------------- /Chapter09/example02/library/remote_copy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | import shutil 4 | 5 | DOCUMENTATION = ''' 6 | --- 7 | module: remote_copy 8 | version_added: future 9 | short_description: Copy a file on the remote host 10 | description: 11 | - The remote_copy module copies a file on the remote host from a given source to a provided destination. 12 | options: 13 | source: 14 | description: 15 | - Path to a file on the source file on the remote host 16 | required: True 17 | dest: 18 | description: 19 | - Path to the destination on the remote host for the copy 20 | required: True 21 | author: 22 | - Jesse Keating 23 | ''' 24 | def main(): 25 | module = AnsibleModule( 26 | argument_spec = dict( 27 | source=dict(required=True, type='str'), 28 | dest=dict(required=True, type='str') 29 | ) 30 | ) 31 | shutil.copy(module.params['source'], 32 | module.params['dest']) 33 | module.exit_json(changed=True) 34 | from ansible.module_utils.basic import * 35 | if __name__ == '__main__': 36 | main() 37 | 38 | -------------------------------------------------------------------------------- /Chapter09/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example02/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | -------------------------------------------------------------------------------- /Chapter09/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-doc -M library/ remote_copy | cat - 10 | -------------------------------------------------------------------------------- /Chapter09/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example03/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | -------------------------------------------------------------------------------- /Chapter09/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_module.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter09/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example04/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | -------------------------------------------------------------------------------- /Chapter09/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-doc -M library/ remote_copy | cat - 10 | -------------------------------------------------------------------------------- /Chapter09/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example05/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | -------------------------------------------------------------------------------- /Chapter09/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_module.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter09/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example06/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | 17 | - name: show a fact 18 | debug: 19 | var: rc_dest 20 | -------------------------------------------------------------------------------- /Chapter09/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_module.yaml -v 10 | -------------------------------------------------------------------------------- /Chapter09/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example07/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | register: mycopy 17 | 18 | - name: set facts from mycopy 19 | set_fact: 20 | rc_dest: "{{ mycopy.dest }}" 21 | -------------------------------------------------------------------------------- /Chapter09/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_module.yaml -v -C 10 | -------------------------------------------------------------------------------- /Chapter09/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example08/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | 17 | - name: show a fact 18 | debug: 19 | var: rc_dest 20 | -------------------------------------------------------------------------------- /Chapter09/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_filter.yaml 10 | -------------------------------------------------------------------------------- /Chapter09/example09/filter_plugins/sample_filter.py: -------------------------------------------------------------------------------- 1 | def cloud_truth(a): 2 | return a.replace("the cloud", "somebody else's computer") 3 | class FilterModule(object): 4 | '''Cloud truth filters''' 5 | def filters(self): 6 | return {'cloud_truth': cloud_truth} 7 | -------------------------------------------------------------------------------- /Chapter09/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example09/simple_filter.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test cloud_truth filter 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | statement: "I store my files in the cloud" 7 | tasks: 8 | - name: make a statement 9 | debug: 10 | msg: "{{ statement | cloud_truth }}" 11 | -------------------------------------------------------------------------------- /Chapter09/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts simple_filter.yaml 10 | -------------------------------------------------------------------------------- /Chapter09/example10/ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | stdout_callback = shrug 3 | -------------------------------------------------------------------------------- /Chapter09/example10/callback_plugins/shrug.py: -------------------------------------------------------------------------------- 1 | from ansible.plugins.callback import default 2 | class CallbackModule(default.CallbackModule): 3 | CALLBACK_VERSION = 2.0 4 | CALLBACK_TYPE = 'stdout' 5 | CALLBACK_NAME = 'shrug' 6 | def v2_on_any(self, *args, **kwargs): 7 | msg = '\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf' 8 | self._display.display(msg.decode('utf-8') * 8) 9 | 10 | -------------------------------------------------------------------------------- /Chapter09/example10/filter_plugins/sample_filter.py: -------------------------------------------------------------------------------- 1 | def cloud_truth(a): 2 | return a.replace("the cloud", "somebody else's computer") 3 | class FilterModule(object): 4 | '''Cloud truth filters''' 5 | def filters(self): 6 | return {'cloud_truth': cloud_truth} 7 | -------------------------------------------------------------------------------- /Chapter09/example10/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/example10/simple_filter.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test cloud_truth filter 3 | hosts: localhost 4 | gather_facts: false 5 | vars: 6 | statement: "I store my files in the cloud" 7 | tasks: 8 | - name: make a statement 9 | debug: 10 | msg: "{{ statement | cloud_truth }}" 11 | -------------------------------------------------------------------------------- /Chapter09/example11/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | time ansible-playbook -i mastery-inventory.py inventory_test.yaml --limit backend,frontend,errors 10 | -------------------------------------------------------------------------------- /Chapter09/example11/ansible.cfg: -------------------------------------------------------------------------------- 1 | [inventory] 2 | enable_plugins = ini, script 3 | -------------------------------------------------------------------------------- /Chapter09/example11/inventory_test.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test the inventory 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: hello world 8 | debug: 9 | msg: "Hello world, I am {{ inventory_hostname }}. 10 | My username is {{ ansible_ssh_user }}" 11 | -------------------------------------------------------------------------------- /Chapter09/example12/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | time ansible-playbook -i mastery-inventory.py inventory_test.yaml --limit backend,frontend,errors 10 | -------------------------------------------------------------------------------- /Chapter09/example12/ansible.cfg: -------------------------------------------------------------------------------- 1 | [inventory] 2 | enable_plugins = ini, script 3 | -------------------------------------------------------------------------------- /Chapter09/example12/inventory_test.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test the inventory 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: hello world 8 | debug: 9 | msg: "Hello world, I am {{ inventory_hostname }}. 10 | My username is {{ ansible_ssh_user }}" 11 | -------------------------------------------------------------------------------- /Chapter10/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | This is a theoretical playbook - application of it is left as an exercise for the reader as it will require an pool of servers to operate against. Some of the variables in the playbook will also need to be defined. 4 | 5 | -------------------------------------------------------------------------------- /Chapter10/example01/library/remote_copy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | import shutil 4 | def main(): 5 | module = AnsibleModule( 6 | argument_spec = dict( 7 | source=dict(required=True, type='str'), 8 | dest=dict(required=True, type='str') 9 | ) 10 | ) 11 | shutil.copy(module.params['source'], 12 | module.params['dest']) 13 | module.exit_json(changed=True) 14 | from ansible.module_utils.basic import * 15 | if __name__ == '__main__': 16 | main() 17 | 18 | -------------------------------------------------------------------------------- /Chapter10/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter10/example01/serial.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Upgrade foo-app in place 3 | hosts: foo-app 4 | serial: 2 5 | 6 | tasks: 7 | - name: disable member in balancer 8 | haproxy: 9 | backend: foo-app 10 | host: "{{ inventory_hostname }}" 11 | state: disabled 12 | delegate_to: foo-lb 13 | 14 | - name: pull stable foo-app 15 | git: 16 | repo: "{{ foo-app.repo }}" 17 | dest: /srv/foo-app/ 18 | version: "{{ foo-version }}" 19 | notify: 20 | - reload nginx 21 | 22 | - meta: flush_handlers 23 | 24 | - name: ensure healthy service 25 | wait_for: 26 | port: 80 27 | 28 | - name: enable member in balancer 29 | haproxy: 30 | backend: foo-app 31 | host: "{{ inventory_hostname }}" 32 | state: enabled 33 | delegate_to: foo-lb 34 | 35 | handlers: 36 | - name: reload nginx 37 | service: 38 | name: nginx 39 | state: restarted 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter10/example01/simple_module.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: test remote_copy module 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: ensure foo 8 | file: 9 | path: /tmp/rcfoo 10 | state: touch 11 | 12 | - name: do a remote copy 13 | remote_copy: 14 | source: /tmp/rcfoo 15 | dest: /tmp/rcbar 16 | -------------------------------------------------------------------------------- /Chapter10/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | This is a theoretical playbook - application of it is left as an exercise for the reader as it will require an OpenStack environment to operate against. Some of the variables in the playbook will also need to be defined. 4 | 5 | -------------------------------------------------------------------------------- /Chapter10/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts failtest.yaml 10 | -------------------------------------------------------------------------------- /Chapter10/example03/failtest.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: any errors fatal 3 | hosts: failtest 4 | gather_facts: false 5 | any_errors_fatal: true 6 | 7 | tasks: 8 | - name: fail last host 9 | fail: 10 | msg: "I am last" 11 | when: inventory_hostname == play_hosts[-1] 12 | - name: never run 13 | debug: 14 | msg: "I should never be run" 15 | when: inventory_hostname == play_hosts[-1] 16 | -------------------------------------------------------------------------------- /Chapter10/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts failtest.yaml 10 | -------------------------------------------------------------------------------- /Chapter10/example04/failtest.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: any errors fatal 3 | hosts: failtest 4 | gather_facts: false 5 | max_fail_percentage: 20 6 | 7 | tasks: 8 | - name: fail last host 9 | fail: 10 | msg: "I am last" 11 | when: inventory_hostname == play_hosts[-1] 12 | - name: never run 13 | debug: 14 | msg: "I should never be run" 15 | when: inventory_hostname == play_hosts[-1] 16 | -------------------------------------------------------------------------------- /Chapter10/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts failtest.yaml 10 | -------------------------------------------------------------------------------- /Chapter10/example05/failtest.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: any errors fatal 3 | hosts: failtest 4 | gather_facts: false 5 | max_fail_percentage: 20 6 | 7 | tasks: 8 | - name: fail last host 9 | fail: 10 | msg: "I am last" 11 | when: inventory_hostname in play_hosts[0:3] 12 | - name: never run 13 | debug: 14 | msg: "I should never be run" 15 | when: inventory_hostname == play_hosts[-1] 16 | -------------------------------------------------------------------------------- /Chapter10/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts failtest.yaml --limit failer01:failer01 10 | -------------------------------------------------------------------------------- /Chapter10/example06/failtest.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: any errors fatal 3 | hosts: failtest 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: run first 8 | debug: 9 | msg: "I am a change" 10 | changed_when: true 11 | when: inventory_hostname == play_hosts[-1] 12 | notify: critical handler 13 | - name: change a host 14 | fail: 15 | msg: "I am last" 16 | when: inventory_hostname == play_hosts[-1] 17 | 18 | - name: never run 19 | debug: 20 | msg: "I should never be run" 21 | when: inventory_hostname == play_hosts[-1] 22 | 23 | handlers: 24 | - name: critical handler 25 | debug: 26 | msg: "I really need to run" 27 | -------------------------------------------------------------------------------- /Chapter10/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts failtest.yaml --limit failer01:failer01 10 | -------------------------------------------------------------------------------- /Chapter10/example07/failtest.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: any errors fatal 3 | hosts: failtest 4 | gather_facts: false 5 | force_handlers: true 6 | 7 | tasks: 8 | - name: run first 9 | debug: 10 | msg: "I am a change" 11 | changed_when: true 12 | when: inventory_hostname == play_hosts[-1] 13 | notify: critical handler 14 | - name: change a host 15 | fail: 16 | msg: "I am last" 17 | when: inventory_hostname == play_hosts[-1] 18 | 19 | - name: never run 20 | debug: 21 | msg: "I should never be run" 22 | when: inventory_hostname == play_hosts[-1] 23 | 24 | handlers: 25 | - name: critical handler 26 | debug: 27 | msg: "I really need to run" 28 | -------------------------------------------------------------------------------- /Chapter10/example07/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts micro.yaml 10 | ansible-playbook -i mastery-hosts micro.yaml -e upgrade=true 11 | -------------------------------------------------------------------------------- /Chapter10/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example08/micro.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: apply microA 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | roles: 7 | - role: microA 8 | 9 | - name: apply microB 10 | hosts: localhost 11 | gather_facts: false 12 | 13 | roles: 14 | - role: microB 15 | 16 | - name: restart microA 17 | hosts: localhost 18 | gather_facts: false 19 | 20 | tasks: 21 | - name: restart microA for upgrade 22 | debug: 23 | msg: "microA is restarting" 24 | when: upgrade | default(false) | bool 25 | 26 | - name: restart microB 27 | hosts: localhost 28 | gather_facts: false 29 | 30 | tasks: 31 | - name: restart microB for upgrade 32 | debug: 33 | msg: "microB is restarting" 34 | when: upgrade | default(false) | bool 35 | -------------------------------------------------------------------------------- /Chapter10/example08/roles/microA/handlers/main.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart microA 3 | debug: 4 | msg: "microA is restarting" 5 | when: not upgrade | default(false) | bool 6 | -------------------------------------------------------------------------------- /Chapter10/example08/roles/microA/tasks/main.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: install microA package 3 | debug: 4 | msg: "This is installing A" 5 | changed_when: true 6 | notify: restart microA 7 | -------------------------------------------------------------------------------- /Chapter10/example08/roles/microB/handlers/main.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart microB 3 | debug: 4 | msg: "microB is restarting" 5 | when: not upgrade | default(false) | bool 6 | -------------------------------------------------------------------------------- /Chapter10/example08/roles/microB/tasks/main.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: install microB package 3 | debug: 4 | msg: "This is installing B" 5 | changed_when: true 6 | notify: restart microB 7 | -------------------------------------------------------------------------------- /Chapter10/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts runonce.yaml 10 | -------------------------------------------------------------------------------- /Chapter10/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter10/example09/runonce.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: run once test 3 | hosts: failtest[0:1] 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: do a thing 8 | debug: 9 | msg: "I am groot" 10 | register: groot 11 | run_once: true 12 | 13 | - name: what is groot 14 | debug: 15 | var: groot 16 | when: inventory_hostname == play_hosts[-1] 17 | -------------------------------------------------------------------------------- /Chapter10/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Run this playbook using the command: 8 | 9 | ansible-playbook -i mastery-hosts forserial.yaml -vv 10 | -------------------------------------------------------------------------------- /Chapter10/example10/forserial.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: parallel and serial 3 | hosts: failtest[0:3] 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: do a thing 8 | debug: 9 | msg: "I am groot" 10 | changed_when: inventory_hostname in play_hosts[0:2] 11 | register: groot 12 | notify: restart groot 13 | 14 | handlers: 15 | - name: restart groot 16 | debug: 17 | msg: "I am groot?" 18 | loop: "{{ play_hosts }}" 19 | delegate_to: "{{ item }}" 20 | run_once: true 21 | when: hostvars[item]['groot']['changed'] | bool 22 | loop_control: 23 | pause: 2 24 | -------------------------------------------------------------------------------- /Chapter10/example10/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | [failtest] 32 | failer[01:10] 33 | -------------------------------------------------------------------------------- /Chapter11/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - OpenStack devstack release March 2019 7 | 8 | Run this playbook using the command (assumes prior setup of OpenStack - adjust the playbook according to your environment): 9 | 10 | ansible-playbook -i mastery-hosts boot-server.yaml -vv 11 | -------------------------------------------------------------------------------- /Chapter11/example01/boot-server.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: boot server 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: boot the server 8 | os_server: 9 | auth: 10 | auth_url: "http://devstack.example.com/identity/v3" 11 | username: "admin" 12 | password: "password" 13 | project_name: "demo" 14 | project_domain_name: "default" 15 | user_domain_name: "default" 16 | flavor: "ds1G" 17 | image: "Fedora 29" 18 | key_name: "mastery-key" 19 | network: "private" 20 | name: "mastery1" 21 | -------------------------------------------------------------------------------- /Chapter11/example01/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - OpenStack devstack release March 2019 7 | 8 | Run this playbook using the command (assumes prior setup of OpenStack - adjust the playbook according to your environment): 9 | 10 | ansible-playbook -i mastery-hosts boot-server.yaml 11 | -------------------------------------------------------------------------------- /Chapter11/example02/boot-server.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: boot server 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: boot the server 8 | os_server: 9 | auth: 10 | auth_url: "http://devstack.example.com/identity/v3" 11 | username: "admin" 12 | password: "password" 13 | project_name: "demo" 14 | project_domain_name: "default" 15 | user_domain_name: "default" 16 | flavor: "ds1G" 17 | image: "Fedora 29" 18 | key_name: "mastery-key" 19 | network: "private" 20 | name: "mastery1" 21 | register: newserver 22 | 23 | - name: show floating ip 24 | debug: 25 | var: newserver.openstack.accessIPv4 26 | -------------------------------------------------------------------------------- /Chapter11/example02/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - OpenStack devstack release March 2019 7 | 8 | Run this playbook using the command (assumes prior setup of OpenStack - adjust the playbook according to your environment): 9 | 10 | ansible-playbook -i mastery-hosts boot-server.yaml --private-key mastery-key 11 | 12 | Note that `mastery-key` should be the private SSH key created in and then downloaded from your OpenStack environment. 13 | -------------------------------------------------------------------------------- /Chapter11/example03/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example04/configure-server.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: configure server 3 | hosts: mastery1 4 | gather_facts: false 5 | remote_user: fedora 6 | 7 | tasks: 8 | - name: install python 9 | raw: "sudo dnf install -y python python2-dnf" 10 | 11 | - name: install imagemagick 12 | dnf: 13 | name: "ImageMagick" 14 | become: "yes" 15 | -------------------------------------------------------------------------------- /Chapter11/example04/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example05/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Amazon EC2 7 | 8 | To run this example, you will need an AWS account, and your credentials for this. Put your credentials where indicated into the playbook, then run it with the command: 9 | 10 | ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i mastery-hosts boot-ec2-server.yaml --private-key mastery-key.pem 11 | 12 | Where mastery-key.pem is your private SSH key downloaded from the keypair you created in EC2. 13 | 14 | -------------------------------------------------------------------------------- /Chapter11/example05/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example06/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Microsoft Azure 7 | 8 | To run this example, you will need an Azure account, and your credentials for this. Put your credentials where indicated into the playbook, then run it with the command: 9 | 10 | ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i mastery-hosts boot-azure-server.yaml 11 | 12 | Note the setup commands you may need from the book before this will run, including: 13 | 14 | sudo pip install azure 15 | sudo pip install ansible[azure] 16 | az login 17 | az vm image list --offer fedora --all --output table 18 | az vm image show --urn tunnelbiz:fedora:fedora29:1.0.0 19 | az vm image accept-terms --urn tunnelbiz:fedora:fedora29:1.0.0 20 | -------------------------------------------------------------------------------- /Chapter11/example06/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example07/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM docker.io/fedora:29 2 | 3 | RUN dnf install -y cowsay nginx 4 | RUN echo "daemon off;" >> /etc/nginx/nginx.conf 5 | RUN cowsay boop > /usr/share/nginx/html/index.html 6 | 7 | EXPOSE 80 8 | 9 | CMD /usr/sbin/nginx 10 | -------------------------------------------------------------------------------- /Chapter11/example07/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Docker version 1.13.1 7 | 8 | This Docker container can be built by simply running the following command in this directory from a user account that has permission to access the Docker daemon: 9 | 10 | docker build . 11 | 12 | -------------------------------------------------------------------------------- /Chapter11/example08/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM docker.io/fedora:29 2 | 3 | RUN dnf install -y cowsay nginx 4 | RUN echo "daemon off;" >> /etc/nginx/nginx.conf 5 | RUN cowsay boop > /usr/share/nginx/html/index.html 6 | 7 | EXPOSE 80 8 | 9 | CMD /usr/sbin/nginx 10 | -------------------------------------------------------------------------------- /Chapter11/example08/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Docker version 1.13.1 7 | 8 | You can check if any related Docker containers are already in existence using the following commands: 9 | 10 | docker ps -a --filter ancestor=fedora-moo 11 | docker images --filter reference='fedora*' 12 | 13 | Then run the playbook as follows to build and run the container: 14 | 15 | ansible-playbook -i mastery-hosts docker-interact.yaml 16 | 17 | Now run the two Docker queries again to see the running container: 18 | 19 | docker ps -a --filter ancestor=fedora-moo 20 | docker images --filter reference='fedora*' 21 | 22 | Also check the container is running with: 23 | 24 | curl http://localhost:8080 25 | 26 | -------------------------------------------------------------------------------- /Chapter11/example08/docker-interact.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: build an image 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: build that image 8 | docker_image: 9 | path: . 10 | state: present 11 | name: fedora-moo 12 | 13 | - name: start the container 14 | docker_container: 15 | name: playbook-container 16 | image: fedora-moo 17 | ports: 8080:80 18 | state: started 19 | -------------------------------------------------------------------------------- /Chapter11/example08/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example09/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Docker version 1.13.1 7 | 8 | You can check if any related Docker containers are already in existence using the following commands: 9 | 10 | docker ps -a --filter ancestor=fedora-moo 11 | 12 | If needs be remove any running containers with this command: 13 | 14 | docker rm -f playbook-container 15 | 16 | Then run the playbook as follows to build and run the container: 17 | 18 | ansible-playbook -i mastery-hosts docker-all.yaml 19 | 20 | Now run the two Docker queries again to see the running container: 21 | 22 | docker ps -a --filter ancestor=fedora:29 23 | 24 | Also check the container is running with: 25 | 26 | curl http://localhost:8080 27 | 28 | -------------------------------------------------------------------------------- /Chapter11/example09/docker-all.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: build an image 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: start the container 8 | docker_container: 9 | name: playbook-container 10 | image: docker.io/fedora:29 11 | ports: 8080:80 12 | state: started 13 | command: sleep 500 14 | 15 | - name: make a host 16 | add_host: 17 | name: playbook-container 18 | ansible_connection: docker 19 | ansible_ssh_user: root 20 | 21 | - name: do things 22 | hosts: playbook-container 23 | gather_facts: false 24 | 25 | tasks: 26 | - name: install things 27 | raw: dnf install -y python-dnf 28 | 29 | - name: install things 30 | dnf: 31 | name: ['nginx', 'cowsay'] 32 | 33 | - name: configure nginx 34 | lineinfile: 35 | line: "daemon off;" 36 | dest: /etc/nginx/nginx.conf 37 | 38 | - name: boop 39 | shell: cowsay boop > /usr/share/nginx/html/index.html 40 | 41 | - name: run nginx 42 | shell: nginx & 43 | -------------------------------------------------------------------------------- /Chapter11/example09/mastery-hosts: -------------------------------------------------------------------------------- 1 | [web] 2 | mastery.example.name ansible_ssh_host=192.168.10.25 3 | 4 | [dns] 5 | backend.example.name 6 | 7 | [database] 8 | backend.example.name 9 | 10 | [frontend:children] 11 | web 12 | 13 | [backend:children] 14 | dns 15 | database 16 | 17 | [web:vars] 18 | http_port=88 19 | proxy_timeout=5 20 | 21 | [backend:vars] 22 | ansible_port=314 23 | ansible_ssh_user=blotto 24 | 25 | [all:vars] 26 | ansible_ssh_user=otto 27 | 28 | [database:vars] 29 | ansible_ssh_user=database 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/example10/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Docker version 1.13.1 7 | 8 | Download the Docker dynamic inventory file as shown: 9 | 10 | curl -O https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/docker.py 11 | 12 | You can test it as shown in the book with these commands: 13 | 14 | ./docker.py --help 15 | ./docker.py --list --pretty | grep -C2 playbook-container 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter11/example11/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Docker version 1.13.1 7 | - Ansible Container version 0.9.2 8 | 9 | Perform the following commands to prepare your system for ansible-container: 10 | 11 | sudo pip uninstall docker-py 12 | sudo pip uninstall docker 13 | sudo pip install docker==2.7.0 14 | sudo sed -i "s/return os.path.join(os.sep, 'run', 'secrets')/return os.path.join(os.sep, 'docker', 'secrets')/g" /usr/lib/python2.7/site-packages/container/docker/engine.py 15 | sudo pip install -U setuptools 16 | sudo pip install "ansible-container[docker,openshift]" 17 | 18 | You can build the container as shown in the book: 19 | 20 | cd ansible 21 | ansible-container build 22 | 23 | Then run the container when built: 24 | 25 | ansible-container run --detached 26 | docker ps --filter name=cowsay 27 | curl http://localhost:8081 28 | 29 | -------------------------------------------------------------------------------- /Chapter11/example11/ansible/ansible-requirements.txt: -------------------------------------------------------------------------------- 1 | # These are the python requirements for your Ansible Container builder. 2 | # You do not need to include Ansible itself in this file. 3 | -------------------------------------------------------------------------------- /Chapter11/example11/ansible/ansible.cfg: -------------------------------------------------------------------------------- 1 | # Set any ansible.cfg overrides in this file. 2 | # See: https://docs.ansible.com/ansible/intro_configuration.html#explanation-of-values-by-section 3 | -------------------------------------------------------------------------------- /Chapter11/example11/ansible/container.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | services: 3 | cowsay: 4 | from: docker.io/fedora:29 5 | conductor_base: fedora:29 6 | roles: 7 | - cowsay 8 | ports: 9 | - "8081:80" 10 | command: ['nginx'] 11 | 12 | -------------------------------------------------------------------------------- /Chapter11/example11/ansible/requirements.yml: -------------------------------------------------------------------------------- 1 | # Install Ansible Roles 2 | # --------------------- 3 | # When making the Conductor image `ansible-galaxy install -r requirements.yml` is executed 4 | # using this file. Follow the instructions at http://docs.ansible.com/ansible/galaxy.html 5 | # to include any roles you want installed prior to running main.yml. 6 | -------------------------------------------------------------------------------- /Chapter11/example11/ansible/roles/cowsay/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: install things 3 | raw: dnf install -y python-dnf 4 | 5 | - name: use local python 6 | set_fact: 7 | ansible_python_interpreter: /usr/bin/python 8 | 9 | - name: install things 10 | dnf: 11 | name: ['nginx', 'cowsay'] 12 | 13 | - name: configure nginx 14 | lineinfile: 15 | line: "daemon off;" 16 | dest: /etc/nginx/nginx.conf 17 | 18 | - name: boop 19 | shell: cowsay boop > /usr/share/nginx/html/index.html 20 | -------------------------------------------------------------------------------- /Chapter12/example01/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | 7 | Note this is a theoretical example - it will need parameters changing if you have an F5 BIG-IP device to test with: 8 | 9 | sudo pip install f5-sdk 10 | ansible-playbook -i mastery-hosts reset-f5.yaml 11 | -------------------------------------------------------------------------------- /Chapter12/example01/reset-f5.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: reset an F5 3 | hosts: localhost 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: reset my F5 8 | bigip_config: 9 | reset: yes 10 | save: yes 11 | server: lb.mastery.example.com 12 | user: admin 13 | password: mastery 14 | validate_certs: no 15 | -------------------------------------------------------------------------------- /Chapter12/example02/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Cumulus VX 3.7.3 7 | 8 | Adjust the inventory file to suit your setup, and then test with: 9 | 10 | ansible -i switch-inventory -m ping mastery-switch1 11 | 12 | We can then query the switch using this playbook: 13 | 14 | ansible-playbook -i switch-inventory switch-query.yaml 15 | 16 | Ports can then be configured using this playbook as discussed in the book: 17 | 18 | ansible-playbook -i switch-inventory switch-l2-configure.yaml 19 | -------------------------------------------------------------------------------- /Chapter12/example02/switch-inventory: -------------------------------------------------------------------------------- 1 | [cumulus] 2 | mastery-switch1 ansible_host=192.168.81.142 3 | 4 | [cumulus:vars] 5 | ansible_user=cumulus 6 | ansible_ssh_pass=CumulusLinux! 7 | -------------------------------------------------------------------------------- /Chapter12/example02/switch-l2-configure.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: configure switch 3 | hosts: mastery-switch1 4 | 5 | tasks: 6 | - name: bring up ports swp[1-3] 7 | nclu: 8 | template: | 9 | {% for interface in range(1,4) %} 10 | add interface swp{{interface}} 11 | add bridge bridge ports swp{{interface}} 12 | {% endfor %} 13 | commit: true 14 | 15 | - name: query swp1 interface 16 | nclu: 17 | commands: 18 | - show interface swp1 19 | register: interface 20 | 21 | - name: print interface status 22 | debug: 23 | var: interface 24 | -------------------------------------------------------------------------------- /Chapter12/example02/switch-query.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: query switch 3 | hosts: mastery-switch1 4 | 5 | tasks: 6 | - name: query swp1 interface 7 | nclu: 8 | commands: 9 | - show interface swp1 10 | register: interface 11 | when: inventory_hostname in groups['cumulus'] 12 | 13 | - name: print interface status 14 | debug: 15 | var: interface 16 | -------------------------------------------------------------------------------- /Chapter12/example03/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Cumulus VX 3.7.3 7 | 8 | Note that this example is theoretical unless you have a Cisco IOS device to test against - however the code is as provided in the book. Configuring this for your environment and running it is left as an exercise for the reader. 9 | 10 | -------------------------------------------------------------------------------- /Chapter12/example03/switch-facts.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "gather device facts" 3 | hosts: all 4 | gather_facts: false 5 | 6 | tasks: 7 | - name: gather ios facts 8 | ios_facts: 9 | when: ansible_network_os == 'ios' 10 | - name: gather cumulus facts 11 | setup: 12 | when: inventory_hostname in groups['cumulus'] 13 | -------------------------------------------------------------------------------- /Chapter12/example03/switch-inventory: -------------------------------------------------------------------------------- 1 | [switches:children] 2 | ios 3 | cumulus 4 | 5 | [ios] 6 | ios01 ansible_host=ios01.mastery.example.com 7 | 8 | [cumulus] 9 | cumulus01 ansible_host=cumulus01.mastery.example.com 10 | 11 | [cumulus:vars] 12 | ansible_user=cumulus 13 | 14 | [ios:vars] 15 | ansible_network_os=ios 16 | ansible_become=yes 17 | ansible_become_method=enable 18 | -------------------------------------------------------------------------------- /Chapter12/example04/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Tested on: 4 | - CentOS 7.6 5 | - Ansible 2.7.5 6 | - Cumulus VX 3.7.3 7 | 8 | This example assumes you already have a jump host set up with hostname `bastion01` and key based SSH authentication set up between this host and the jump host. When this is in place: 9 | 10 | Adjust the inventory file to suit your setup, and then test with: 11 | 12 | ansible -i switch-inventory -m ping mastery-switch1 13 | 14 | We can then query the switch using this playbook: 15 | 16 | ansible-playbook -i switch-inventory switch-query.yaml 17 | 18 | Ports can then be configured using this playbook as discussed in the book: 19 | 20 | ansible-playbook -i switch-inventory switch-l2-configure.yaml 21 | -------------------------------------------------------------------------------- /Chapter12/example04/switch-inventory: -------------------------------------------------------------------------------- 1 | [cumulus] 2 | mastery-switch1 ansible_host=192.168.81.142 3 | 4 | [cumulus:vars] 5 | ansible_user=cumulus 6 | ansible_ssh_pass=CumulusLinux! 7 | ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q bastion01"' 8 | -------------------------------------------------------------------------------- /Chapter12/example04/switch-l2-configure.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: configure switch 3 | hosts: mastery-switch1 4 | 5 | tasks: 6 | - name: bring up ports swp[1-3] 7 | nclu: 8 | template: | 9 | {% for interface in range(1,4) %} 10 | add interface swp{{interface}} 11 | add bridge bridge ports swp{{interface}} 12 | {% endfor %} 13 | commit: true 14 | 15 | - name: query swp1 interface 16 | nclu: 17 | commands: 18 | - show interface swp1 19 | register: interface 20 | 21 | - name: print interface status 22 | debug: 23 | var: interface 24 | -------------------------------------------------------------------------------- /Chapter12/example04/switch-query.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: query switch 3 | hosts: mastery-switch1 4 | 5 | tasks: 6 | - name: query swp1 interface 7 | nclu: 8 | commands: 9 | - show interface swp1 10 | register: interface 11 | when: inventory_hostname in groups['cumulus'] 12 | 13 | - name: print interface status 14 | debug: 15 | var: interface 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------