Difference between revisions of "Ansible playbooks"
(14 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Ansible playbooks<ref>https://docs.ansible.com/ansible/latest/user_guide/playbooks.html</ref> are written in [[ | + | Ansible playbooks<ref>https://docs.ansible.com/ansible/latest/user_guide/playbooks.html</ref> are written in [[YAML]] text files and define actions to execute against host/devices, check official Ansible playbook introduction: https://docs.ansible.com/ansible/devel/user_guide/playbooks_intro.html |
=== Task definitions or Playbooks === | === Task definitions or Playbooks === | ||
Task definitions are defined in text files, called playbooks in Ansible terminology, in [[DevOps/YAML|YAML]] format. Execution of task definitions or playbooks are make using <code>ansible-playbook</code> command followed by your desired playbook to execute. Playbooks can also use [[Jinja]] templates to enable dynamic expressions and access to variables<ref>https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html</ref>. | Task definitions are defined in text files, called playbooks in Ansible terminology, in [[DevOps/YAML|YAML]] format. Execution of task definitions or playbooks are make using <code>ansible-playbook</code> command followed by your desired playbook to execute. Playbooks can also use [[Jinja]] templates to enable dynamic expressions and access to variables<ref>https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html</ref>. | ||
− | + | Ansible Playbook execution: | |
* <code>ansible-playbook my_new_created_playbook.yml</code> | * <code>ansible-playbook my_new_created_playbook.yml</code> | ||
* <code>ansible-playbook my_new_created_playbook.yml -f 10</code> -f 10 parallelize 10 executions | * <code>ansible-playbook my_new_created_playbook.yml -f 10</code> -f 10 parallelize 10 executions | ||
Line 12: | Line 12: | ||
Add the following line <code>#!/usr/bin/env ansible-playbook</code> at the beginning of your playbooks and make it executable <code>chmod +x /path/to/your/playbook.yml</code> to execute without calling ansible-playbook binary. | Add the following line <code>#!/usr/bin/env ansible-playbook</code> at the beginning of your playbooks and make it executable <code>chmod +x /path/to/your/playbook.yml</code> to execute without calling ansible-playbook binary. | ||
− | === Ansible Playbooks Examples=== | + | === [[Ansible Playbooks Examples]]=== |
Create a new file ''touch /tmp/test_file.txt'', REMOTE_SERVER should be already present in your [[Ansible inventory]]:<code>/etc/ansible/host</code> | Create a new file ''touch /tmp/test_file.txt'', REMOTE_SERVER should be already present in your [[Ansible inventory]]:<code>/etc/ansible/host</code> | ||
Playbook using module <code>file</code><ref>https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module</ref>: | Playbook using module <code>file</code><ref>https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module</ref>: | ||
− | + | ||
− | - hosts: REMOTE_SERVER_HOSTNAME | + | #![[/usr/bin/env]] ansible-playbook |
− | + | ||
− | + | - hosts: REMOTE_SERVER_HOSTNAME | |
− | + | tasks: | |
− | + | - name: Ansible create a new file called test_fle.txt in /tmp directory | |
− | + | file: | |
− | + | path: "/tmp/test_file.txt" | |
− | Important Note: dash symbol (-) and indentation has to be properly formatted. | + | state: touch |
+ | |||
+ | Important Note: dash symbol (-) and [[indentation]] has to be properly formatted. | ||
+ | |||
+ | Additional examples: | ||
+ | * [[Installing sysstat using Ansible]] | ||
+ | |||
+ | |||
+ | == Errors == | ||
+ | [[A playbook must be a list of plays]] | ||
=== Variables === | === Variables === | ||
Line 33: | Line 42: | ||
# Read Ansible introduction to playbooks: https://docs.ansible.com/ansible/devel/user_guide/playbooks_intro.html | # Read Ansible introduction to playbooks: https://docs.ansible.com/ansible/devel/user_guide/playbooks_intro.html | ||
# Review different options for running playbooks, including: <code>--limit</code> | # Review different options for running playbooks, including: <code>--limit</code> | ||
+ | # Review <code>--check</code> option | ||
+ | # Review [[Ansible playbooks examples]] | ||
== See also == | == See also == | ||
− | * | + | * {{ansible}} |
[[Category:Server administration]] | [[Category:Server administration]] |
Latest revision as of 10:10, 18 October 2022
Ansible playbooks[1] are written in YAML text files and define actions to execute against host/devices, check official Ansible playbook introduction: https://docs.ansible.com/ansible/devel/user_guide/playbooks_intro.html
Contents
Task definitions or Playbooks[edit]
Task definitions are defined in text files, called playbooks in Ansible terminology, in YAML format. Execution of task definitions or playbooks are make using ansible-playbook
command followed by your desired playbook to execute. Playbooks can also use Jinja templates to enable dynamic expressions and access to variables[2].
Ansible Playbook execution:
ansible-playbook my_new_created_playbook.yml
ansible-playbook my_new_created_playbook.yml -f 10
-f 10 parallelize 10 executions
-f forks
Create "Executable" Playbooks[edit]
Add the following line #!/usr/bin/env ansible-playbook
at the beginning of your playbooks and make it executable chmod +x /path/to/your/playbook.yml
to execute without calling ansible-playbook binary.
Ansible Playbooks Examples[edit]
Create a new file touch /tmp/test_file.txt, REMOTE_SERVER should be already present in your Ansible inventory:/etc/ansible/host
Playbook using module file
[3]:
#!/usr/bin/env ansible-playbook - hosts: REMOTE_SERVER_HOSTNAME tasks: - name: Ansible create a new file called test_fle.txt in /tmp directory file: path: "/tmp/test_file.txt" state: touch
Important Note: dash symbol (-) and indentation has to be properly formatted.
Additional examples:
Errors[edit]
A playbook must be a list of plays
Variables[edit]
You can use variables in your playbooks, check official documentation for further information: https://docs.ansible.com/ansible/devel/user_guide/playbooks_variables.html
Activities[edit]
- Read Ansible introduction to playbooks: https://docs.ansible.com/ansible/devel/user_guide/playbooks_intro.html
- Review different options for running playbooks, including:
--limit
- Review
--check
option - Review Ansible playbooks examples
See also[edit]
- Ansible: modules, plugins, Playbooks (examples)
ansible-playbook
,ansible-vault
,ansible-inventory
,ansible-config
, Ansible Tower, Ansible Galaxy (Roles) (ansible-galaxy
),ansible-cmdb
,gather facts
,ansible.cfg
,Ansible Molecule
, Ansible collections,register
,template
,--ask-pass
,--ask-become-pass
,remote_user:
,/etc/ansible/hosts
,ansible-doc
,ansible-lint
,.ansible/
,--forks
,--start-at-task
,changelog
,inventory
,Notify:
,ansible HOSTNAME -m ping
,gathering
,/usr/bin/ansible
,ansible -m ping
,ansible.builtin
,hosts: (Ansible)
,set fact:
,when:
,blockinfile:
,become method:
,include:
,git:
, AWX,ansible --help
, Tags, Ansible variables, versions
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.
Source: wikiversity
Advertising: