I have a role in ansible working in command line but not through awx.
Here the role :
- name: Enable persistent logging
ansible.builtin.lineinfile:
path: /etc/systemd/journald.conf
regexp: '^#Storage'
line: Storage=persistent
- name: Check directory
ansible.builtin.stat:
path: "{{ journal_dir }}"
register: journaldir
- block:
- name: Create directory
ansible.builtin.file:
path: "{{ journal_dir }}"
state: directory
mode: '0755'
- name: Enable systemd-tmpfiles folder
ansible.builtin.command: /bin/systemd-tmpfiles --create --prefix {{ journal_dir }}
check_mode: no
notify:
- restart systemd-journald
when: journaldir.stat.exists == false and ansible_distribution_major_version >= '7'
Here the notify code :
- name: restart systemd-journald
ansible.builtin.service:
name: systemd-journald
state: restarted
{{ journal_dir }} is /var/log/journal
I have no issue when I run the playbook on my terminal, but when I run it with awx, I still have this error :
TASK [journalctl : Enable systemd-tmpfiles folder] *****************************
fatal: [server]: FAILED! => {"changed": false, "msg": "no command given", "rc": 256}
I have done test also with shell module, it's the same behaviour.
And I don't understand why.
thank you for your help.
I foud the issue, it seems I'm using an old version of ansible, so I had to remove the fqcn for the command module.
It works like this :
- name: Enable systemd-tmpfiles folder
command: /bin/systemd-tmpfiles --create --prefix {{ journal_dir }}
notify:
- restart systemd-journald
my ansible version through command line is 2.9.27,ansible version of awx is 2.9.14.
[solution has been found here] : Ansible cant run any command or shell
Related
Ansible 2.11.0
I created anI: disk partition on my Windows 2019 host under jenkins user, who is an admin. Logged in as jenkins, I can create a directory on the partition.
I have this Ansible task that simply tries to create the same directory structure, like this
- name: "Create data directory {{ pg_data_dir }}"
win_file:
path: "{{ pg_data_dir }}" # I:\pgdata\13
status: directory
I get ...
TASK [postgresql : Create data directory I:\pgdata\13] ********************************
task path: /path/ansible/exa-playbooks/roles/postgresql/tasks/install_postgresql.yml:21
redirecting (type: modules) ansible.builtin.win_file to ansible.windows.win_file
Using module file /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_file.ps1
Pipelining is enabled.
<10.227.xx.xx> ESTABLISH WINRM CONNECTION FOR USER: jenkins on PORT 5986 TO 10.227.xx.xx
EXEC (via pipeline wrapper)
fatal: [10.227.xx.xx]: FAILED! => {
"changed": false,
"msg": "path I:\\pgdata\\13 will not be created"
}
Any clues?
UPDATE
For now, I just changed my task to this, but the above issue persists.
- name: "Create data directory {{ pg_data_dir }}"
win_shell:
mkdir "{{ pg_data_dir }}" # I:\pgdata\13
Duh on me. It should be state, not status, like this
- name: "Create data directory {{ pg_data_dir }}"
win_file:
path: "{{ pg_data_dir }}" # I:\pgdata\13
state: directory
I am using Ansible AWX to issue a restart command to restart an apache2 service on a host. The restart command is contained in a playbook.
---
- name: Manage Linux Services
hosts: all
tasks:
- name: Restart a linux service
command: systemctl restart '{{ service_name }}'
register: result
ignore_errors: yes
- name: Show result of task
debug:
var: result
OR
---
- name: Manage Linux Services
hosts: all
tasks:
- name: Restart a linux service
ansible.builtin.service:
name: '{{ service_name }}'
state: restarted
register: result
ignore_errors: yes
- name: Show result of task
debug:
var: result
However, when I run the command, I get the error below:
"Failed to restart apache2.service: Connection timed out",
"See system logs and 'systemctl status apache2.service' for details."
I have tried to figure out the issue, but no luck yet.
I later figured the cause of the issue.
Here's how I fixed it:
The restart command requires sudo access to run which was missing in my command.
All I have to do was to add the become: true command so that I can execute the command with root privileges.
So my playbook looked like this thereafter:
---
- name: Manage Linux Services
hosts: all
tasks:
- name: Restart a linux service
command: systemctl restart '{{ service_name }}'
become: true
register: result
ignore_errors: yes
- name: Show result of task
debug:
var: result
OR
---
- name: Manage Linux Services
hosts: all
tasks:
- name: Restart a linux service
ansible.builtin.service:
name: '{{ service_name }}'
state: restarted
become: true
register: result
ignore_errors: yes
- name: Show result of task
debug:
var: result
Another way if you want to achieve this on Ansible AWX is to tick the Privilege Escalation option in the job template.
If enabled, this runs the selected playbook in the job template as an administrator.
That's all.
I hope this helps
Restarting a service requires sudo privileges. Besides adding the 'become' directive, if you would like to prompt for the password, you can do so by passing the -K flag (note: uppercase K)
$ ansible-playbook myplay.yml -i hosts -u myname --ask-pass -K
I am using a ansible playbook and it is running fine on my local system but when I run the same playbook on a remote server then it fails due to a error given by chdir module i.e "msg": [Errno 2] No such file or directory, "rc": 2.
Please if anyone can help me figure out what is the exact issue here.
- hosts: all
vars:
test: "Test Successfull"
repo_dir: /media/disk1/sandbox/xyz
path: /media/disk1/sandbox/xyz/api
tasks:
- debug:
msg: "{{ test.split()[0] }} {{ test.split()[1] }}"
- name: Running npm install in directory "{{ path }} and {{ repo_dir }}/lib as well"
command: npm install
args:
chdir: "{{ item }}"
loop:
- "{{ path }}"
- "{{ repo_dir }}/lib"
become_user: yash
become: yes
Please try as below.
- name: Running npm install
npm:
path:"{{ package_path }}"
executable: "{{ npm_path }}"
where {{ package_path }} is path to package.json file and {{npm_path}} is /usr/bin/npm( as per your npm path)
I'm trying to set up a role in ansible to install some servers with needed applications. One of the apps is docker.
Docker-ce is installed successfully. Now I'm trying to tell the system to startup docker.service and enable it by reboot.
When I'm creating a list over "with_items" it works fine, when I'm trying to use a list out of my defaults/main.yml file ansible tells me that it can't find the service docker. Now I'm wondering, maybe just some spelling problem?
This one works fine
- name: Start and enable needed services
systemd:
name: "{{ item }}"
state: started
enabled: yes
daemon_reload: yes
with_items:
- docker
This one doesn't work
- name: Start and enable needed services
systemd:
name: "{{ clientonline }}"
state: started
enabled: yes
daemon_reload: yes
-------
# in defaults/main.yml
clientonline:
- docker
Ansible can't find the docker service when I'm using my list from defaults/main.yml
[WARNING]: The value ['docker'] (type list) in a string field was converted to u"['docker']" (type string). If this does not look like what you expect, quote the entire value to ensure it does not change.
Also this example doesn't work:
- name: Start and enable needed services
systemd:
name: "{{ item }}"
state: started
enabled: yes
daemon_reload: yes
with_items:
- clientonline
That brings this error:
failed: [fgi_appdeploy_server] (item=clientonline) => {"ansible_loop_var": "item", "changed": false, "item": "clientonline", "msg": "Could not find the requested service clientonline: host"}
that will work:
- name: Start and enable needed services
systemd:
name: "{{ item }}"
state: started
enabled: yes
daemon_reload: yes
with_item:
- "{{ clientonline }}"
since clientonline is a list you need to loop through it
OK; this now works fine for systemd:
- name: Start and enable needed services
systemd:
name: '{{ item }}'
state: started
enabled: yes
daemon_reload: yes
with_items:
- '{{ clientonline }}'
But same style for yum module will bring this warning on bash, nice to know
- name: Install needed packages
yum:
name: '{{ item }}'
state: latest
with_items:
- '{{ clientpackages }}'
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['{{ serverpackages }}']` and remove the loop. This feature will
be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
Nice to know - thanks to all!
I have a private Docker registry runnning in 172.20.20.20 in port 5000. And I have pushed the image pruebasjeje to it.
Now, I need to pull and run the image using Ansible task:
- name: run container
docker:
registry: 172.20.20.20:5000
insecure_registry: true
image: pruebasjeje
state: reloaded
pull: always
ports:
- "8080:8080"
But when I execute it, I get this error:
fatal: [localhost]: FAILED! => {"changed": false, "changes":
["{\"status\":\"Pulling repository
docker.io/library/pruebasjeje\"}\r\n",
"{\"errorDetail\":{\"message\":\"Error: image
library/pruebasjeje:latest not found\"},\"error\":\"Error: image
library/pruebasjeje:latest not found\"}\r\n"], "failed": true, "msg":
"Unrecognized status from pull.", "status": ""}
It seems that is looking for the image in docker.io. How should I write my task in order to do what I want?
Thanks.
Not sure if it is how it should be done, but this is how I do it:
- name: run container
docker:
insecure_registry: true
image: 172.20.20.20:5000/pruebasjeje
state: reloaded
pull: always
ports:
- "8080:8080"
I just started with Docker myself. Here are the tasks I have for setting up Docker on my target host (CentOS 7):
- name: Install epel repo
yum:
name: "http://download.fedoraproject.org/pub/epel/{{ ansible_distribution_major_version }}/{{ ansible_userspace_architecture }}{{ '/' if ansible_distribution_major_version < '7' else '/e/' }}epel-release-{{ ansible_distribution_major_version }}-5.noarch.rpm"
state: present
become: yes
- name: Install Docker
yum:
name: "{{ item }}"
state: latest
enablerepo: epel
with_items:
- device-mapper
- docker-engine
- python-pip
become: yes
- name: Install docker-py
pip:
name: docker-py
become: yes
- name: Add registry cert
copy:
src: registry.crt
dest: /etc/docker/certs.d/{{ docker_registry_host }}/ca.crt
become: yes
- name: Allow access to insecure registry
lineinfile:
dest: /usr/lib/systemd/system/docker.service
regexp: ^ExecStart=
line: ExecStart=/usr/bin/docker daemon -H fd:// --insecure-registry {{ docker_registry_host }}:5000
become: yes
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
become: yes