ansible
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| ansible [2026/03/01 11:39] – [Inventory and Config] reddy | ansible [2026/03/01 19:14] (current) – reddy | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| </ | </ | ||
| - | ==== Inventory and Config ==== | + | ===== Inventory and Config |
| The config is read from: ANSIBLE_CONFIG, | The config is read from: ANSIBLE_CONFIG, | ||
| + | |||
| + | Ansible will not automatically load a config file from the current working directory if the directory is world-writable. | ||
| < | < | ||
| Line 42: | Line 44: | ||
| [web] | [web] | ||
| - | webserver1 | + | webserver1 ansible_host=webserver1.localdomain ansible_port=2022 ansible_user=webadmin |
| localhost ansible_connection=local | localhost ansible_connection=local | ||
| Line 58: | Line 60: | ||
| # | # | ||
| # | # | ||
| + | |||
| + | # Before Ansible 2.0, ansible_user was ansible_ssh_user | ||
| </ | </ | ||
| + | |||
| + | ===== Executing ===== | ||
| + | |||
| + | For ad-hoc execution: | ||
| + | |||
| + | < | ||
| + | ansible localhost -m ansible.builtin.gather_facts | ||
| + | |||
| + | ansible -i inventory all -m shell -a 'echo " | ||
| + | |||
| + | ansible -a ' | ||
| + | |||
| + | ansible -i inventory all -m shell -a 'echo " | ||
| + | |||
| + | # -f 5 # To run 5 tasks at a time | ||
| + | </ | ||
| + | |||
| + | For playbook execution: | ||
| + | |||
| + | < | ||
| + | ansible-playbook playbook1.yaml | ||
| + | </ | ||
| + | |||
| + | ===== Playbooks ===== | ||
| + | |||
| + | < | ||
| + | --- | ||
| + | - name: Play 1 | ||
| + | become: true | ||
| + | hosts: all | ||
| + | tasks: | ||
| + | | ||
| + | - name: Task 1 | ||
| + | yum_repository: | ||
| + | name: epel | ||
| + | description: | ||
| + | baseurl: https:// | ||
| + | gpgcheck: no | ||
| + | | ||
| + | - name: Task 2 | ||
| + | dnf: | ||
| + | name: httpd | ||
| + | state: latest | ||
| + | |||
| + | - name: Task 2 | ||
| + | dnf: | ||
| + | name: | ||
| + | - net-tools | ||
| + | - bind-utils | ||
| + | - telnet | ||
| + | - nmap | ||
| + | - vim-enhanced | ||
| + | - sysstat | ||
| + | - tuned | ||
| + | - numactl | ||
| + | | ||
| + | - name: Task 3 | ||
| + | dnf: name=http:// | ||
| + | | ||
| + | - name: Task 4 | ||
| + | apt_repository: | ||
| + | repo: deb http:// | ||
| + | state: present | ||
| + | | ||
| + | - name: Task 5 | ||
| + | apt: | ||
| + | name: foo | ||
| + | update_cache: | ||
| + | state: present | ||
| + | | ||
| + | - name: Task 6 | ||
| + | apt: | ||
| + | deb: https:// | ||
| + | | ||
| + | - name: Task 7 | ||
| + | copy: | ||
| + | src: file1 | ||
| + | dest: /tmp/file1 | ||
| + | remote_src: true | ||
| + | | ||
| + | - name: Task 8 | ||
| + | template: | ||
| + | src: file1.j2 | ||
| + | dest: /tmp/file1 | ||
| + | vars: | ||
| + | var1: 1 | ||
| + | | ||
| + | - name: Task 8 | ||
| + | shell: | | ||
| + | command1 | ||
| + | command2 | ||
| + | | ||
| + | - name: Task 7 | ||
| + | file: | ||
| + | path: /tmp/dir1 | ||
| + | state: directory | ||
| + | owner: root | ||
| + | group: root | ||
| + | mode: " | ||
| + | | ||
| + | - name: Task 8 | ||
| + | sysctl: | ||
| + | name: net.ipv4.ip_forward | ||
| + | value: ' | ||
| + | sysctl_set: true | ||
| + | state: present | ||
| + | reload: true | ||
| + | | ||
| + | - name: Task 8 | ||
| + | selinux: | ||
| + | policy: targeted | ||
| + | state: permissive | ||
| + | | ||
| + | - name: Task 8 | ||
| + | service: | ||
| + | name: firewalld | ||
| + | state: stopped | ||
| + | enabled: no | ||
| + | | ||
| + | - name: Task 8 | ||
| + | get_url: | ||
| + | url: http:// | ||
| + | dest: /tmp/file1 | ||
| + | environment: | ||
| + | http_proxy: https:// | ||
| + | https_proxy: | ||
| + | | ||
| + | - name: Task 6 | ||
| + | local_action: | ||
| + | | ||
| + | - name: Task 6 | ||
| + | script: / | ||
| + | | ||
| + | - name: Task 6 | ||
| + | lineinfile: | ||
| + | path: /tmp/file1 | ||
| + | regexp: ' | ||
| + | line: db=localhost | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | # Tasks to get the output from one command and use as conditional for another command | ||
| + | |||
| + | - name: Check ping | ||
| + | delegate_to: | ||
| + | shell: ping -c 1 somehost | ||
| + | register: reachableping | ||
| + | ignore_errors: | ||
| + | |||
| + | - name: Do something | ||
| + | delegate_to: | ||
| + | shell: ./run_cmd_1 | ||
| + | ignore_errors: | ||
| + | when: "' | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | # Variables can be used for an entire block | ||
| + | |||
| + | - name: Demonstrate environment variable in block | ||
| + | hosts: localhost | ||
| + | connection: local | ||
| + | gather_facts: | ||
| + | vars: | ||
| + | motd: Hello World | ||
| + | tasks: | ||
| + | - block: | ||
| + | |||
| + | - name: Print variable | ||
| + | shell: "echo $motd" | ||
| + | register: theoutput | ||
| + | |||
| + | - debug: var=theoutput.stdout | ||
| + | environment: | ||
| + | motd: "{{ motd }}" | ||
| + | </ | ||
| + | |||
| + | ===== Ansible Navigator ===== | ||
| + | |||
| + | Ansible Navigator uses podman to create an EE environment to run the Ansible playbooks and provides a text user interface to navigate through the documentation, | ||
| + | |||
| + | Install Ansible Navigator with: | ||
| + | |||
| + | < | ||
| + | pip3 install ansible-dev-tools ansible-navigator --user | ||
| + | |||
| + | # or | ||
| + | |||
| + | dnf install \ | ||
| + | --enablerepo=ansible-automation-platform-2.2-for-rhel-8-x86_64-rpms \ | ||
| + | ansible-navigator | ||
| + | </ | ||
| + | |||
| + | Use it with: | ||
| + | |||
| + | < | ||
| + | ansible-navigator run playbook1.yaml | ||
| + | |||
| + | ansible-navigator doc service | ||
| + | |||
| + | ansible-navigator doc service -m stdout | ||
| + | </ | ||
| + | |||
| + | ===== Common Ansible Problems ===== | ||
| + | |||
| + | < | ||
| + | ERROR: Ansible requires the locale encoding to be UTF-8; Detected ISO8859-1. | ||
| + | |||
| + | Set the following environment variables within the shell before running ansible / ansible-playbook | ||
| + | |||
| + | LANG=" | ||
| + | LC_CTYPE=" | ||
| + | </ | ||
| + | |||
| + | ===== Also See ===== | ||
| + | |||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | |||
ansible.1772361574.txt.gz · Last modified: by reddy
