I created an Ansible playbook to configure :
- a database server
- a nginx & php server
The database server needs to know the php server ip address to configure database access rights.
The webserver needs to know the database server ip address to configure the database access for the website.
I tried with delegate_facts :
vars:
Database_server_name: "SVWEB-03"
Web_server_name: "SVWEB-02"
- name: Web server ip address
set_fact:
Web_server_ip: "{{ ansible_default_ipv4.address }}"
delegate_to: "{{ Web_server_name }}"
delegate_facts: True
when:
- "Database_server_name in inventory_hostname"
- name: Database server ip address
set_fact:
Database_server_ip: "{{ ansible_default_ipv4.address }}"
delegate_to: "{{ Database_server_name }}"
delegate_facts: True
when:
- "Web_server_name in inventory_hostname"
- debug:
msg: "{{ Web_server_ip }}"
when:
- "Database_server_name in inventory_hostname"
- debug:
msg: "{{ Database_server_ip }}"
when:
- "Web_server_name in inventory_hostname"
But I have errors :
TASK [Web server ip address] ***************************************************************************************************
skipping: [SVWEB-02]
ok: [SVWEB-03 -> SVWEB-02]
TASK [Database server ip address] ************************************************************************************************
skipping: [SVWEB-03]
ok: [SVWEB-02 -> SVWEB-03]
TASK [debug] ***************************************************************************************************************
skipping: [SVWEB-02]
fatal: [SVWEB-03]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'Web_server_ip' is undefined\n\nThe error appears to have been in '/home/murmure/ansible/playbooks/Install_Wordpress_Separate_DB_and_Web.yml': line 64, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}
TASK [debug] ***************************************************************************************************************
fatal: [SVWEB-02]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'Database_server_ip' is undefined\n\nThe error appears to have been in '/home/murmure/ansible/playbooks/Install_Wordpress_Separate_DB_and_Web.yml': line 69, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}
Does someone have an idea about my mistake please ?
I believe what you need is in hostvars:
- name: Database server ip address
set_fact:
Database_server_ip: "{{ hostvars.Database_server_name.ansible_default_ipv4.address }}"
Related
I am new to ansible. I am trying to get info from an esxi about the VM installed on it. No vcentre available.
I have created the playbook. Connection is working however I am getting the error in the Task debug. Any help is much appreciated. Thanks
Playbook:
- hosts: esxi
gather_facts: false
become: false
tasks:
- name: Gather all registered virtual machines
community.vmware.vmware_vm_info:
hostname: '{{ hostname }}'
username: '{{ ansible_user }}'
password: '{{ password }}'
validate_certs: no
delegate_to: localhost
register: vminfo
- debug:
msg: "{{ item.guest_name }}, {{ item.ip_address }}"
with_items:
- "{{ vminfo.virtual_machines }}"
The Error is:
TASK [debug] *************************************************************************************************************************
fatal: [192.168.233.202]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/home/sciclunam/task4/playbook11.yaml': line 14, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}
PLAY RECAP ***************************************************************************************************************************
192.168.233.202 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
The correct indentation is very necessary in Ansible.
Your with_items is not correctly indented.
Please notice the indentation of wiith_items
Here is my sample
# Create Directory Structure
- name: Create directory application
file:
path: "{{item}}"
state: directory
mode: 0755
with_items:
- "/opt/project1"
- "/opt/project1/script"
- "/opt/project1/application"
Can I run on specific host or group of hosts in a Ansible task?
---
- hosts: all
become: yes
tasks:
- name: Disable tuned
hosts: client1.local
service:
name: tuned
enabled: false
state: stopped
It does not work anyway. Here is the error:
[root#centos7 ansible]# ansible-playbook playbook/demo.yaml
ERROR! conflicting action statements: hosts, service
The error appears to be in '/root/ansible/playbook/demo.yaml': line 24, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Disable tuned
^ here
For example
- hosts: test_11,test_12,test_13
vars:
client1:
showroom: [test11, test12]
local: [test_13]
tasks:
- debug:
var: inventory_hostname
- debug:
msg: "Local client: {{ inventory_hostname }}"
when: inventory_hostname in client1.local
gives
TASK [debug] ****************************************************************
ok: [test_11] =>
inventory_hostname: test_11
ok: [test_12] =>
inventory_hostname: test_12
ok: [test_13] =>
inventory_hostname: test_13
TASK [debug] ****************************************************************
skipping: [test_11]
skipping: [test_12]
ok: [test_13] =>
msg: 'Local client: test_13'
Since I had the similar requirement to structure hosts into groups I found the following approach working for me.
First I've structured my inventory according my environment, administrative groups and tasks.
inventory
[infrastructure:children]
prod
qa
dev
[prod:children]
tuned_hosts
[tuned_hosts]
client1.local
Then I can use in
playbook
---
- hosts: all
become: yes
tasks:
- name: Disable tuned
service:
name: tuned
enabled: false
state: stopped
when: ('tuned_hosts' in group_names) # or ('prod' in group_names)
as well something like
when: ("dev" not in group_names)
depending on what I try to achieve.
Documentation
How to build your inventory
Special Variables
Playbook Conditionals
Using ansible I want to check in which port tomcat is running, of course I think there are different ways to do it, but I found this ansible module
https://docs.ansible.com/ansible/latest/modules/listen_ports_facts_module.html
And I would like to use it, but according to the examples, I do not know how to use it.
I mean if I setup
gather_facts: true
And run the task
- name: List TCP ports
debug:
msg: "{{ ansible_facts.tcp_listen }}"
I got the error
TASK [discover-servers : List TCP ports] *******************************************************************************************************************************
task path: /home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml:4
fatal: [2a00:da9:2:21ca:111:0:426:2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'tcp_listen'\n\nThe error appears to be in '/home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: List TCP ports\n ^ here\n"}
If I set up the task exactly how it is in the example, I got an error in the first task Gather facts on listening ports
TASK [discover-servers : include_tasks] ********************************************************************************************************************************
task path: /home/A78252689/sap_bo/roles/discover-servers/tasks/main.yml:4
fatal: [2a00:da9:2:21ca:111:0:426:2]: FAILED! => {"reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to be in '/home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Gather facts on listening ports\n ^ here\n"}
So, do you know how to use the module listen_ports_facts?
Thanks in advance for your kind support
The gather_facts stage of your playbook runs uses the setup module. It does not run listen_ports_facts, so if you don't run the module explicitly you won't have those facts available.
From the docs, the listen_ports_module creates the following facts:
tcp_listen
udp_listen
Using Ansible 2.9.2, the following works just fine:
---
- gather_facts: false
hosts: localhost
tasks:
- listen_ports_facts:
- debug:
msg: "{{ tcp_listen }}"
- debug:
msg: "{{ udp_listen }}"
If you're trying to run the listen_ports_facts module and you're getting the error "no action detected in task", it may be that you're running an older version of Ansible that doesn't have the listen_ports_facts module. It first showed up in version 2.9.
I am trying to restart jboss on server_group except cgfmgr, its failing. can some please help me with the syntax
TASK:
- name: restarted jboss
service: name=jboss state=restarted enabled=yes
when: inventory_hostname in groups["{{ server_group }}:!cfgmgr-{{ server_group }}"]
Error-
TASK: [restarted jboss]
******************************************************* fatal: [ansible] => error while evaluating conditional: inventory_hostname in
groups["sit:!cfgmgr-sit"] FATAL: all hosts have already failed --
aborting
Hostfile
[sit:children]
jboss-sit
cfgmgr-sit
webserver-sit
You can't use patterns when accessing groups' elements, only group names.
Try it with group_names magic variable:
- name: restarted jboss
service: name=jboss state=restarted enabled=yes
when: server_group in group_names and ('cfgmgr-'+server_group) not in group_names
This code is not tested.
I wrote a playbook to modify the IP address of several remote systems. I wrote the playbook to change only a few systems at a time, so I wanted to use delegate_to to change the DNS record on the nameservers as each system was modified, instead of adding a separate play targeted at the nameservers that would change all the host IPs at once.
However, it seems the handler is being run on the primary playbook target, not my delegate_to target. Does anyone have recommendations for working around this?
Here's my playbook:
---
host: hosts-to-modify
serial: 1
tasks:
- Modify IP for host-to-modify
//snip//
- name: Modify DNS entry
delegate_to: dns-servers
become: yes
replace:
args:
backup: yes
regexp: '^{{ inventory_hostname }}\s+IN\s+A\s+[\d\.]+$'
replace: "{{ inventory_hostname }} IN A {{ new_ip }}"
dest: /etc/bind/db.my.domain
notify:
- reload dns service
handlers:
- name: reload dns service
become: yes
service:
args:
name: bind9
state: reloaded
With an inventory file like the following:
[dns-servers]
ns01
ns02
[hosts-to-modify]
host1 new_ip=10.1.1.10
host2 new_ip=10.1.1.11
host3 new_ip=10.1.1.12
host4 new_ip=10.1.1.13
Output snippet, including error message:
TASK [Modify DNS entry] ********************************************************
Friday 02 September 2016 14:46:09 -0400 (0:00:00.282) 0:00:35.876 ******
changed: [host1 -> ns01]
changed: [host1 -> ns02]
RUNNING HANDLER [reload dns service] *******************************************
Friday 02 September 2016 14:47:00 -0400 (0:00:38.925) 0:01:27.385 ******
fatal: [host1]: FAILED! => {"changed": false, "failed": true, "msg": "no service or tool found for: bind9"}
First of all, you example playbook is invalid in several ways: play syntax is flawed and delegate_to can't be targeted to a group of hosts.
If you want to delegate to multiple servers, you should iterate over them.
And answering your main question: yes, you can use delegate_to with handlers:
handlers:
- name: reload dns service
become: yes
service:
args:
name: bind9
state: reloaded
delegate_to: "{{ item }}"
with_items: "{{ groups['dns-servers'] }}